• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Sorting-Java Comparator-predefined list

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
}
 
It's feeding time! Give me the food you were going to give to this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic