# sorting collection of integers

• (2 Pages)
• 1
• 2

## 16 Replies - 12185 Views - Last Post: 17 June 2009 - 06:32 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=107893&amp;s=7bf63e183c7adb8cb2ed994ba31af905&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mb1980

Reputation: 0
• Posts: 67
• Joined: 07-May 09

# sorting collection of integers

Posted 31 May 2009 - 05:35 PM

How do i sort this so the integers are ordered in reverse anybody can give me an example program?

```
import java.util.*;
class Sorting {
public static void main(String[] args) {
int listSize = 30;
int magnitude = 500;
Random rand = new Random(12345);
ArrayList<Integer> numbers = new ArrayList<Integer>(listSize);
for (int i=0; i < listSize; i++)
Collections.sort(numbers);
for (Integer num : numbers)
System.out.print(num + " ");
System.out.println();
}
}

```

Is This A Good Question/Topic? 0

## Replies To: sorting collection of integers

### #2 333OnlyHalfEvil

Reputation: 24
• Posts: 664
• Joined: 20-March 09

## Re: sorting collection of integers

Posted 31 May 2009 - 05:51 PM

Well, you already sort the list so the numbers should be in normal order. Just make a new list and transfer the numbers in backwards.

This post has been edited by 333OnlyHalfEvil: 31 May 2009 - 05:52 PM

### #3 Tanira

Reputation: 10
• Posts: 102
• Joined: 30-May 09

## Re: sorting collection of integers

Posted 31 May 2009 - 06:27 PM

Stacks are done in reverse order. If you really wanted to you could throw it in a stack. Yay for data structures!

### #4 g00se

• D.I.C Lover

Reputation: 2998
• Posts: 12,849
• Joined: 20-September 08

## Re: sorting collection of integers

Posted 01 June 2009 - 01:50 AM

```Collections.sort(numbers, Collections.reverseOrder() );

```

### #5 mb1980

Reputation: 0
• Posts: 67
• Joined: 07-May 09

## Re: sorting collection of integers

Posted 01 June 2009 - 07:55 AM

like this:

```
import java.util.*;
class Sorting {
public static void main(String[] args) {
int listSize = 30;
int magnitude = 500;
Random rand = new Random(12345);
ArrayList<Integer> numbers = new ArrayList<Integer>(listSize);
for (int i=0; i < listSize; i++)
Collections.sort(numbers, Collections.reverseOrder() );
for (Integer num : numbers)
System.out.print(num + " ");
System.out.println();
}
}

```

and then i have to create another class called reverseOrder? i am sorry i am just new to this and trying to get into it please let me know what i am doing wrong..... THX i really have to say this webside is awesome!!!

This post has been edited by mb1980: 01 June 2009 - 07:56 AM

### #6 g00se

• D.I.C Lover

Reputation: 2998
• Posts: 12,849
• Joined: 20-September 08

## Re: sorting collection of integers

Posted 01 June 2009 - 08:16 AM

Quote

and then i have to create another class called reverseOrder?

No, that's part of the api. You need to import java.util.Collections

### #7 sbromley

Reputation: 21
• Posts: 127
• Joined: 20-May 09

## Re: sorting collection of integers

Posted 01 June 2009 - 08:38 AM

A fundamental way to reverse the order of something in a List is to use a stack.

Check out this link for a great explanation and example implementation of a stack.

http://hamilton.bell...es/notes_12.pdf

Hope this helps!

### #8 mb1980

Reputation: 0
• Posts: 67
• Joined: 07-May 09

## Re: sorting collection of integers

Posted 15 June 2009 - 05:10 PM

I am getting an error message when i compile what am i doing wrong???

ERROR MESSAGE:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method main cannot be declared static; static methods can only be declared in a static or top level type

at BackwardOrdering\$SortingBeck.main(SortComp.java:23)

```
import java.util.*;

class AlternatingOrdering  implements Comparator {

public int compare(Object o1, Object o2) {
Integer val1 = (Integer)10;
Integer val2 = (Integer)11;
if ((val1 > 0) || (val2 > 0)) {
return val1.compareTo(val2);
} else {
return val1.compareTo(val2) * -1;
}
}
}

class BackwardOrdering implements Comparator {

public int compare(Object 10, Object 11) {
return ((Integer)o1).compareTo((Integer)o2) * -1;
}
}
class SortingBeck {
public static void main(String[] args) {
int listSize = 50;
int magnitude = 400;
Random rand = new Random(34567);
ArrayList<Integer> numbers = new ArrayList<Integer>(listSize);
for (int i = 0; i < listSize; i++)
Collections.sort(numbers);
for (Integer num : numbers)
System.out.print(num + " ");
System.out.println();

// BackwardOrdering
Collections.sort(numbers, new BackwardOrdering());
for (Integer num : numbers)
System.out.print(num + " ");
System.out.println();

Collections.sort(numbers, new AlternatingOrdering());
for (Integer num : numbers)
System.out.print(num + " ");
System.out.println();
}
}

```

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: sorting collection of integers

Posted 15 June 2009 - 05:22 PM

g00se, on 1 Jun, 2009 - 12:50 AM, said:

```Collections.sort(numbers, Collections.reverseOrder() );

```

was about time somebody take it out the magician hat

### #10 mb1980

Reputation: 0
• Posts: 67
• Joined: 07-May 09

## Re: sorting collection of integers

Posted 15 June 2009 - 06:13 PM

What did i do wrong i still do not understand?

### #11 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: sorting collection of integers

Posted 15 June 2009 - 06:18 PM

mb1980, on 15 Jun, 2009 - 05:13 PM, said:

What did i do wrong i still do not understand?

You did nothing wrong
the solution was easy and presented by G00se

http://java.sun.com/...(java.util.List, java.util.Comparator)

### #12 mb1980

Reputation: 0
• Posts: 67
• Joined: 07-May 09

## Re: sorting collection of integers

Posted 15 June 2009 - 06:22 PM

still not helping me but thx i will find it somehow i am new to this, you helped me before thx mate.

### #13 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: sorting collection of integers

Posted 15 June 2009 - 08:05 PM

mb1980, on 15 Jun, 2009 - 05:22 PM, said:

still not helping me but thx i will find it somehow i am new to this, you helped me before thx mate.

You have 3 classes in your .java file

AlternatingOrdering
BackwardOrdering
SortingBeck

when multiple classes are on the same .java file the public static void main() method must be in the first class

so move SortingBeck before the 2 other ones

### #14 mb1980

Reputation: 0
• Posts: 67
• Joined: 07-May 09

## Re: sorting collection of integers

Posted 16 June 2009 - 06:30 AM

Ok i did that and did some changes it compiles fine and the output looks fine also. There are just some things that eclipse is showing some exclamation marks !!!! what do i need to change?

```
import java.util.*;
import java.util.Collections;

class SortingComp {
public static void main(String[] args) {
int listSize = 50;
int magnitude = 400;
Random rand = new Random(34567);
ArrayList<Integer> numbers = new ArrayList<Integer>(listSize);
for (int i = 0; i < listSize; i++)
Collections.sort(numbers,Collections.reverseOrder());
for (Integer num : numbers)
System.out.print(num + " ");
System.out.println();

class AlternatingOrdering  implements Comparator {

public int compare(Object o1, Object o2) {
Integer val1 = (Integer)10;
Integer val2 = (Integer)11;
if ((val1 > 0) || (val2 > 0)) {
return val1.compareTo(val2);
} else {
return val1.compareTo(val2) * -1;
}
}
}

class BackwardOrdering implements Comparator  {

public int compare(Object o1, Object o2)
{
return ((Integer)o1).compareTo((Integer)o2) * -1;
}
}

// BackwardOrdering
Collections.sort(numbers, new BackwardOrdering());
for (Integer num : numbers)
System.out.print(num + " ");
System.out.println();

Collections.sort(numbers, new AlternatingOrdering());
for (Integer num : numbers)
System.out.print(num + " ");
System.out.println();
}
}

```

### #15 333OnlyHalfEvil

Reputation: 24
• Posts: 664
• Joined: 20-March 09

## Re: sorting collection of integers

Posted 16 June 2009 - 10:47 AM

Do you mean exclamation marks on the side? Those can be from a variety of things. If your program runs fine, I wouldn't worry about it. If you're a perfectionist then hold your mouse over the part of the line of code that has a yellow line under it and based on what it says try to figure out how to fix it.