Pairs { public static List<EncodedPair> sortByNameAndValues(List<? extends EncodedPair> pairs){ List<EncodedPair> sorted = new ArrayList<EncodedPair>(pairs); Collections.sort(sorted, PAIR_NAME_VALUE_COMPARATOR); return sorted; } private  Pairs(); static EncodedPair toPreEncodedPair(String name, String value); static EncodedPair toPair(String nameToEncode, String valueToEncode, Charset charset); static EncodedPair toPair(String name, String value, Charset charset, boolean encoded); static List<EncodedPair> fromUrlEncoded(String urlEncoded); static List<EncodedPair> sortByNameAndValues(List<? extends EncodedPair> pairs); static String join(List<? extends EncodedPair> pairs, char pairSep); static String join(List<? extends EncodedPair> pairs, char pairSep, char nameValSep); static String join(List<? extends EncodedPair> pairs, char pairSep, char nameValSep, boolean quoteName, boolean quoteVal); static String join(Iterator<? extends EncodedPair> pairs, char pairSep); static String join(Iterator<? extends EncodedPair> pairs, char pairSep, char nameValSep); static String join(Iterator<? extends EncodedPair> pairs, char pairSep, char nameValSep, boolean quoteName, boolean quoteVal); static void join(Writer writer, List<? extends EncodedPair> pairs, char pairSep); static void join(Writer writer, List<? extends EncodedPair> pairs, char pairSep, char nameValSep); static void join(Writer writer, List<? extends EncodedPair> pairs, char pairSep, char nameValSep, boolean quoteName, boolean quoteVal); static void join(Writer writer, Iterator<? extends EncodedPair> pairs, char pairSep); static void join(Writer writer, Iterator<? extends EncodedPair> pairs, char pairSep, char nameValSep); static void join(Writer writer, Iterator<? extends EncodedPair> pairs, char pairSep, char nameValSep, boolean quoteName, boolean quoteVal);  }
@Test public void sortByNameAndValuesShouldSort(){ List<? extends EncodedPair> expected = asList( new SimpleEncodedPair("a", "aa"), new SimpleEncodedPair("a", "ab"), new SimpleEncodedPair("aa", "aa"), new SimpleEncodedPair("aa", "ab"), new SimpleEncodedPair("ab", ""), new SimpleEncodedPair("ab", "00"), new SimpleEncodedPair("ab", "01") ); List<? extends EncodedPair> toSort = new ArrayList<EncodedPair>(expected); Collections.shuffle(toSort); List<? extends EncodedPair> toSortOriginal = new ArrayList<EncodedPair>(toSort); List<EncodedPair> actual = Pairs.sortByNameAndValues(toSort); assertEquals(toSortOriginal, toSort); assertEquals(expected, actual); }