Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting-Java Comparator-predefined list

 
suresh saro
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic