0

Is it possible for treemap??, actually treemap itself sort on the keys but I want to sort keys as well as values also.

TreeMap <Double, List<String>> treemap = new TreeMap <Double, List<String>>();  

Example

 Keys : 1.84, 2.35, 5.89, 0.21  
 values: {Burger, 02058795247}, {Pizza, 02087958742}, {Rolls, 020547896874}, {Sandwich, 02058967412} 

Result should be

keys : 0.21  
Values: {Sandwich, 02058967412}  
keys : 0.21, 1.84 
Values: {Sandwich, 02058967412}, {Burger, 02058795247}  
keys : 0.21, 1.84, 2.35  
Values: {Sandwich, 02058967412}, {Burger, 02058795247}, {Pizza, 02087958742}  
keys : 0.21, 1.84, 2.35, 5.89   
Values: {Sandwich, 02058967412}, {Burger, 02058795247}, {Pizza, 02087958742}, {Rolls, 020547896874}

But I got result like

   keys : 0.21    
   values: {Burger, 02058795247}  
   Key: 0.21, 1.84  
   Value : {Burger, 02058795247, Pizza, 02087958742}  
   keys : 0.21, 1.84, 2.35  
   Value: {Burger, 02058795247, Pizza, 02087958742, Rolls, 020547896874}
   keys : 0.21, 1.84, 2.35, 5.89  
   Value :{Burger, 02058795247, Pizza, 02087958742, Rolls, 020547896874, Sandwich, 02058967412}
abc
  • 67
  • 3
  • 11
  • yes you can sort it.... I*ll post the answer soon. – RajSharma Jul 13 '15 at 05:24
  • possible duplicate of [TreeMap sort by value](http://stackoverflow.com/questions/2864840/treemap-sort-by-value) – John Jul 13 '15 at 05:27
  • Not got the solution from this given link because I used List as value. So how to use comparator for value? @user3360241 – abc Jul 13 '15 at 05:37
  • i think you must follow counting sort principle for key and value. i.e key is a first digit and second digit is value and then it can be sorted as string array. http://www.sanfoundry.com/java-program-implement-counting-sort/ – MPG Jul 13 '15 at 06:10
  • When ? @CodeProcessor – abc Jul 13 '15 at 06:15
  • @abc Let me see if i got this right, you would like to do natural order sorting by keys and also you would like to sort the List value attached to a key by some criteria ? – John Jul 13 '15 at 06:24
  • Yes, correct. you are right @user3360241 – abc Jul 13 '15 at 06:32
  • Since you are inserting keys together with values why not either: a) sort list before you combine it with key, or b) use TreeSet instead of list with appropriate comparator – John Jul 13 '15 at 06:58
  • I want to sort list according to sorted keys & I can't use TreeSet because I have to set this map to expandable listview adapter. @user3360241 – abc Jul 13 '15 at 11:33

3 Answers3

0

Sort afterwards.

Take a list of key-value entries, and sort it using a custom Comparator. Unfortunately the value is a list of strings, maybe a SortedSet<String> might be easier.

List<Map.Entry<Double, List<String>>> entries = new ArrayList<>(treemap.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<Double, List<String>>>() {
    @Override
    int compareTo(Map.Entry<Double, List<String>> lhs,
            Map.Entry<Double, List<String>> rhs) {
        int cmp = Double.compare(lhs.getKey(), rhs.getKey());
        if (cmp == 0) {
            Iterator<String> lit = lhs.getValue().iterator();
            Iterator<String> rit = rhs.getValue().iterator();
            while (cmp == 0) {
                 boolean lhas = lit.hasNext();
                 boolean rhas = rit.hasNext();
                 if (!lhas && !rhas) {
                     break;
                 }
                 if (!lhas) {
                     cmp = -1;
                 } else if (!rhas) {
                     cmp = 1;
                 } else {
                     cmp == lit.next().compareTo(rit.next());
                 }
            }
        }
        return cmp;
    });

for (Map.Entry> entry : entries) { ... entry }

Maybe List<String> should itself be a SortedSet<String>, a TreeSet<String>.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

You can use this method:

private static Map<Double, List<String>> sortByComparator(Map<Double, List<String>> unsortMap)
    {

        List<Map.Entry<Double, List<String>>> list = new LinkedList<>(unsortMap.entrySet());

        // Sorting the list based on values
        Collections.sort(list, new Comparator<Map.Entry<Double, List<String>>>()
        {
            public int compare(Map.Entry<Double, List<String>> o1,
                               Map.Entry<Double, List<String>> o2)
            {
                if(o1.getKey() == o2.getKey()) return 0;
                return (o1.getKey() < o2.getKey() == true ? -1 : 1);
            }
        });

        // Maintaining insertion order with the help of LinkedList
        Map<Double, List<String>> sortedMap = new LinkedHashMap<>();
        for (Map.Entry<Double, List<String>> entry : list)
        {
            sortedMap.put(entry.getKey(), entry.getValue());
        }

        return sortedMap;
    }
Kinnar Vasa
  • 397
  • 1
  • 9
  • Thanks but I already use that its not working for my condition @KinnarVasa – abc Jul 13 '15 at 06:05
  • It seems like, you haven't try to run above code, please do one try and check output. – Kinnar Vasa Jul 13 '15 at 06:27
  • @abc, really strange!! – Kinnar Vasa Jul 13 '15 at 06:34
  • I just run code and got output as: {0.21=[Swandwitch, 756334], 1.84=[Bruger, 234234234], 2.35=[Pizza, 342342], 5.89=[Rolls, 453453]} – Kinnar Vasa Jul 13 '15 at 06:34
  • How? when i tried this code so I got 0.21={Burger, 02058795247}, 1.84={Pizza, 02087958742}, 2.35={Rolls, 020547896874}, 5.89={Sandwich, 02058967412}. I mean when I insert data into arraylist so I got same that insertion order not get desirable output @KinnarVasa – abc Jul 13 '15 at 06:56
  • List list = new ArrayList<>(); list.add("Bruger"); list.add("234234234"); ... add all your items... Map> sortList = sortByComparator(treemap); Log.e("Sort Item :", "Sort List: "+ sortList.toString()); – Kinnar Vasa Jul 13 '15 at 07:05
0

Here is entire code:

public class InformationList extends Activity
{
    TreeMap<Double, List<String>> treemap = new TreeMap <Double, List<String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        List<String> list = new ArrayList<>();
        list.add("Bruger");
        list.add("234234234");

        treemap.put(1.84, list);

        List<String> list1 = new ArrayList<>();
        list1.add("Pizza");
        list1.add("342342");
        treemap.put(2.35, list1);

        List<String> list2 = new ArrayList<>();
        list2.add("Rolls");
        list2.add("453453");
        treemap.put(5.89, list2);

        List<String> list3 = new ArrayList<>();
        list3.add("Swandwitch");
        list3.add("756334");
        treemap.put(0.21, list3);

        Map<Double, List<String>> sortList = sortByComparator(treemap);
        Log.e("Sort Item :",  "Sort List: "+ sortList.toString());

    }

    private static Map<Double, List<String>> sortByComparator(Map<Double, List<String>> unsortMap)
    {

        List<Map.Entry<Double, List<String>>> list = new LinkedList<>(unsortMap.entrySet());

        // Sorting the list based on values
        Collections.sort(list, new Comparator<Map.Entry<Double,List<String>>>() {
            public int compare(Map.Entry<Double, List<String>> o1,
                               Map.Entry<Double, List<String>> o2) {
                if (o1.getKey() == o2.getKey()) return 0;
                return (o1.getKey() < o2.getKey() == true ? -1 : 1);
            }
        });

        // Maintaining insertion order with the help of LinkedList
        Map<Double, List<String>> sortedMap = new LinkedHashMap<>();
        for (Map.Entry<Double, List<String>> entry : list)
        {
            sortedMap.put(entry.getKey(), entry.getValue());
        }

        return sortedMap;
    }
}
Kinnar Vasa
  • 397
  • 1
  • 9