Jump to content

Sorting an integer list


spoonraker

Recommended Posts

Now, I'm not completely stupid, I know how to just straight up sort a list of integers, but this is more complicated.What I need to do, is take a list of IDs (not the integers being sorted), iterate through that list and pull out the number associated with each ID from a MySQL database, sort the numbers, and then output a list of just the IDs, sorted by the number previously pulled out.I had this same problem before, only the data being sorted was a string. So what I did was create a hashtable. I made the String to be sorted (with the ID appended on the end) the key, and I made the ID the value. I created a vector from the key set, sorted it, and then iterated through it to pull all the values out of the hashtable (IDs) into a new sorted vector, it worked great.However now that I'm dealing with numbers, things are complicated. Hashtables can not have duplicate keys, and before I just appened the ID onto the string to be sorted, but if I do that with an integer it will create a new number and throw off the sort. I can not handle it as a string and them remove the appended part when I go to sort either, because then I lose my original key to look up the IDs after the sort. Also, I can not switch my hashtable data (make the ID the key and the sorted data the value) because Hashtables don't have any reverse lookup methods. i.e. I can't send in a value and return the key.Somebody recommended that I try a LinkedList, which sounded good after a bit of reading, but it didn't work either. I need to keep the IDs linked to the numbers, so when I try to use the ID (which is an int btw) as the index and the number as the value, I got IndexOutOfBounds exception. The IDs are 6 or 7 digit numbers, so I don't think just allocating a ton of space to it would be a good idea, when probably no more than a few hundred indexs will be assigned values.So.....any ideas?I need something that will link an ID (can be String or int) to an integer value, allow me to pull the numbers out and sort them, and then reference the IDs using the values from the sorted list as the lookup criteria.

Link to comment
Share on other sites

the best thing I can come up with is to create a hashtable which uses a simple ascending integer list as keys, and the ID as the values, then make a vector of the data to be sorted, using the same indexes as the hash table. Copy that vector and sort it, then iterate through the sorted vector and perform a reverse lookup (pull the index) on the non-sorted vector, then use that index to reference the hashtable and finally pull out the SFIID.This will work I'm sure of it, but this can't possibly be that complicated. I know I'm not that first person that has needed to sort a map by the value rather than the key.

Link to comment
Share on other sites

Answer given on another forum, worked great, just thought others might want this info in case they have a similar problem.Steps :1) Create a TreeMap and store values in it.2) Create a List with values from TreeMap.entrySet().3) Implement Comparator interface and apply your camparison constriants.4) Sort the list.So just do as follows:Create 1 class with name : sortValues.javaimport java.util.ArrayList;import java.util.Collections;import java.util.Iterator;import java.util.List;import java.util.TreeMap;public class sortValues{public static void main(String args[]){TreeMap hm= new TreeMap();hm.put(new Integer(100),new Integer(1000));hm.put(new Integer(200),new Integer(900));hm.put(new Integer(300),new Integer(800));hm.put(new Integer(400),new Integer(700));hm.put(new Integer(500),new Integer(600));hm.put(new Integer(600),new Integer(500));hm.put(new Integer(700),new Integer(400));hm.put(new Integer(800),new Integer(300));hm.put(new Integer(900),new Integer(200));hm.put(new Integer(1000),new Integer(100));hm.put(new Integer(1100),new Integer(100));hm.put(new Integer(10),new Integer(10));List entrylist = new ArrayList(hm.entrySet());Collections.sort(entrylist,new ValueComparator());Iterator i = entrylist.iterator();while(i.hasNext()){System.out.println(i.next());}}}Another class : ValueComparator.javaimport java.util.Comparator;import java.util.Iterator;import java.util.Map;class ValueComparator implements Comparator {public int compare(Object o1, Object o2) {Map.Entry e1 = (Map.Entry) o1;Map.Entry e2 = (Map.Entry) o2;Comparable c1 = (Comparable)e1.getValue();Comparable c2 = (Comparable)e2.getValue();return c1.compareTo(c2);}}***Note*** : Your ending list will be of the type Map.Entry. To get a listing of just the ID values you will have to cast them properly. If you are iterating through the list, it would be like this ((Integer)((Map.Entry)iterataor.next()).getKey()).intValue()

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...