aspose file tools*
The moose likes Java in General and the fly likes Sorting-Java Comparator-predefined list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting-Java Comparator-predefined list" Watch "Sorting-Java Comparator-predefined list" New topic
Author

Sorting-Java Comparator-predefined list

suresh saro
Greenhorn

Joined: May 22, 2006
Posts: 4
I want to sort the items in my list with a predefined order....Is it possible? I can sort my List using a natural sort as in the below example. But that is not am looking for.....Am looking for sorting my list using an already defined order which i can hold using another list. IS IT POSSIBLE USING Comparator

import java.util.*;

public class TestUsingMe {
static class AppComparator implements
Comparator {
public int compare(Object o1, Object o2) {
int cc = ((Integer)o1).compareTo(o2);
return (cc < 0 ? 1 : cc > 0 ? -1 : 0);
}
}

static Comparator appcomparator =
new AppComparator();

static void sort(List list1) {
Collections.sort(list1, appcomparator);
}

public static void main(String[] args) {
List list1 = new ArrayList();
list1.add(new Integer(90));
list1.add(new Integer(43));
list1.add(new Integer(21));

sort(list1);

System.out.println(list1);
}
}

Please reply................

[ June 05, 2006: Message edited by: suresh saro ]
[ June 09, 2006: Message edited by: suresh saro ]
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
It would help if you refine your question. Are you looking to have the elements in the collection presorted (sorted while inserting)? ArrayList do not implement internal sort. If you want your elements to be sorted you have to use TreeSet or Hashtable. TreeSet can take Comparator as an argument.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Sure if your comparator has a reference to that other list. However it gets tricky because some collections don't like their sort order changing after items are entered and wont respect that change. You may have to sort as you put them into a new sorted list if the order will change after the insert.
suresh saro
Greenhorn

Joined: May 22, 2006
Posts: 4
Yes, I managed to sort.......as in the program below....But my requirement is, I have the List that needs to be sorted, which is a list of DTO's with an attribute inside that DTO. This attribute is the criteria for sorting my List and this list needs to be sorted as predefined in the list2...as in the program....Is it possible. If possible, how is it?? Can anyone please help me.......

import java.util.*;
public class TestUsingMe {
public static void main(String[] args) {
List list1 = new ArrayList();
list1.add("Named Non-Owner");
list1.add("Restored");
list1.add("Antique/Classic");

List list2 = new ArrayList();
list2.add("Private Passenger");
list2.add("Restored");
list2.add("Antique/Classic");
list2.add("Named Non-Owner");
list2.add("Trailer");

AppComparator appcomparator = new AppComparator(list2);
Collections.sort(list1, appcomparator);

System.out.println(list1);
System.out.println(list2);
}

static class AppComparator implements Comparator {
private List list;

public AppComparator(List list) {
this.list = list;
}

public int compare(Object o1, Object o2) {
return list.indexOf(o1) - list.indexOf(o2);
}
}
}
 
jQuery in Action, 2nd edition
 
subject: Sorting-Java Comparator-predefined list