Search code examples
sortingdictionarytreemapbubble-sort

how to sort a treemap using bubble sort?


27527-683
27525-1179
27525-1571
27525-1813
27525-4911
27526-1303
27526-3641
27525-3989
27525-4083
27525-4670
27526-4102
27526-558
27527-2411
27527-4342

this is the list of key where it is declared as string in a map
then i want to sort it in ascending order.
how can i use a bubble sorting method inside a map?
where the value of the key is a list. in order to get :

27525-1179
27525-1571
27525-1813
27525-3989
27525-4083
27525-4670
27525-4911
27526-558
27526-1303
27526-3641
27526-4102
27527-683
27527-2411
27527-4342


Solution

  • You should be able to just perform an in-order traversal on your tree. Bu if you insist here is what you would do.

    keyList = yourTreeMap.getKeys();
    for(i = keyList.length-1; i > 0; i--)
        for(j = 0; j < i; j++)
           if (keyList[j] > keyList[j+1]) keyList.swap(j, j+1);
    

    Since you don't specify a lnaguage, I present psuedocode.