How can I sort NameValuePair
objects like this by key
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(7)
nameValuePairs.add(new BasicNameValuePair("api_key", "1"));
nameValuePairs.add(new BasicNameValuePair("merchant", "4"));
nameValuePairs.add(new BasicNameValuePair("format", "json"));
nameValuePairs.add(new BasicNameValuePair("method", method));
nameValuePairs.add(new BasicNameValuePair("cid", "0"));
Pass in the Comparator which sorts two NameValuePair
s by key. Something like
Comparator<NameValuePair> comp = new Comparator<NameValuePair>() { // solution than making method synchronized
@Override
public int compare(NameValuePair p1, NameValuePair p2) {
return p1.getName().compareTo(p2.getName());
}
}
// and then
Collections.sort(pairs, comp);
In Java 8 you can use
Collections.sort(pairs, Comparator.comparing(NameValuePair::getName));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With