/**
*
*/
package de.tutorials;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
/**
* @author Tom
*
*/
public class SortExample {
/**
* @param args
*/
public static void main(String[] args) {
class KeyValuePair implements Comparable<KeyValuePair> {
double value;
String name;
public KeyValuePair(String name, double value) {
this.name = name;
this.value = value;
}
public int compareTo(KeyValuePair o) {
return (int) (o.value - this.value);
}
public String toString() {
return this.name + ": " + this.value;
}
}
KeyValuePair[] keyValuePairs = new KeyValuePair[5];
keyValuePairs[0] = new KeyValuePair("A", 1024.0);
keyValuePairs[1] = new KeyValuePair("B", 80);
keyValuePairs[2] = new KeyValuePair("C", 1255);
keyValuePairs[3] = new KeyValuePair("D", 48);
keyValuePairs[4] = new KeyValuePair("E", 2);
Arrays.sort(keyValuePairs);
System.out.println(Arrays.toString(keyValuePairs));
Set<KeyValuePair> set = new TreeSet<KeyValuePair>();
set.add(new KeyValuePair("A", 1024.0));
set.add(new KeyValuePair("B", 80));
set.add(new KeyValuePair("C", 1255));
set.add(new KeyValuePair("D", 48));
set.add(new KeyValuePair("E", 2));
System.out.println(set);
}
}