sorting collection of integers

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 11152 Views - Last Post: 17 June 2009 - 06:32 AM Rate Topic: -----

#1 mb1980  Icon User is offline

  • D.I.C Head

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

sorting collection of integers

Post icon  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++)
	  numbers.add(rand.nextInt() % magnitude);
	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  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 Tanira  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: sorting collection of integers

Posted 01 June 2009 - 01:50 AM

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


Was This Post Helpful? 0
  • +
  • -

#5 mb1980  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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++)
	  numbers.add(rand.nextInt() % magnitude);
	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

Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 sbromley  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#8 mb1980  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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++)
			numbers.add(rand.nextInt() % magnitude);
		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();
	}
}




Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: sorting collection of integers

Posted 15 June 2009 - 05:22 PM

View Postg00se, on 1 Jun, 2009 - 12:50 AM, said:

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


:^: was about time somebody take it out the magician hat
Was This Post Helpful? 0
  • +
  • -

#10 mb1980  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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?
can u please explain?
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: sorting collection of integers

Posted 15 June 2009 - 06:18 PM

View Postmb1980, on 15 Jun, 2009 - 05:13 PM, said:

What did i do wrong i still do not understand?
can u please explain?

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

http://java.sun.com/...(java.util.List, java.util.Comparator)
Was This Post Helpful? 0
  • +
  • -

#12 mb1980  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: sorting collection of integers

Posted 15 June 2009 - 08:05 PM

View Postmb1980, 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
Was This Post Helpful? 0
  • +
  • -

#14 mb1980  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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++)
			numbers.add(rand.nextInt() % magnitude);
		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();
	}
}



Was This Post Helpful? 0
  • +
  • -

#15 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2