4

I'm interested in an efficient way to store pairs of numbers, and to sort them according to a value of one of the numbers. Say I have a list of numbers:

(1, 2), (3, 5), (4, 3), (7, 8)

These pairs need to be stored in some way, then sorted in descending order of the second number, such that the order of these pairs is

(7, 8), (3, 5), (4, 3), (1, 2)

What would be the Java code to achieve this? I'm aware of C++'s std::pair, but I was wondering about the procedure in Java.

user506901
  • 699
  • 4
  • 9
  • 18
  • Sorting based on Values : http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java http://stackoverflow.com/questions/11647889/sorting-the-mapkey-value-in-descending-order-based-on-the-value – Bharath Mg Feb 05 '14 at 11:44
  • 8
    `Collections.sort()` using a `List<>` of your own `Tuple` class objects and a `Comparator`? – Anders R. Bystrup Feb 05 '14 at 11:44
  • @Anders R. Bystrup Can you please write a sample Java code that achieves this? thanks – user506901 Feb 05 '14 at 11:46
  • 1
    @user506901 Yes I could, but then you wouldn't learn anything, would you? – Anders R. Bystrup Feb 05 '14 at 11:46
  • @Anders R.Bystrup Could you please consider my update? I'm trying to achieve a descending order. Thanks – user506901 Feb 05 '14 at 12:51
  • How are your "tuples" represented? An own class like `MyIntPair`, or are this small 2-element-arrays or Lists....? Or are all numbers stored in ONE "flat" 1D array or list? – Marco13 Feb 05 '14 at 14:58

3 Answers3

1

You can use Multimap to store the pair (1, 2), (3, 5), (4, 3), (7, 8) as key 1 and value 2 for first pair.Then use comparator to sort the map according to the value of map.

Kick
  • 4,823
  • 3
  • 22
  • 29
1

you can use a TreeMap and store the values in a reverse order. so the second values of the paris will be the key of the Map.

stinepike
  • 54,068
  • 14
  • 92
  • 112
  • 2
    This will get complicated if the the second value of the pairs is no longer unique. Than you would need to store lists as values of the keys. – MrSmith42 Feb 05 '14 at 13:35
1

Make a class pair which implements comparable interface and use arraylist and collections.sort to sort it.

Example :-

public class pair implements Comparable<pair> {

            int a,b;
            @Override
            public int compareTo(pair o) {
                return(o.b-b); 
            }

            public pair(int a,int b) {

               this.a = a ;
               this.b = b;

            }

            public String toString() {
                return "("+a+","+b+")";

            }


            public static void main(String[] args) {

                 ArrayList pairs =  new ArrayList();
                 pairs.add(new pair(4,5));
                 pairs.add(new pair(7,8));
                 pairs.add(new pair(1,3));
                 Collections.sort(pairs);
                 System.out.println("sorted: "+pairs);

            }


        }
Vikram Bhat
  • 6,106
  • 3
  • 20
  • 19