I need to sort a Set of String\'s which holds number.Ex: [15, 13, 14, 11, 12, 3, 2, 1, 10, 7, 6, 5, 4, 9, 8]
. I need to sort it to [1, 2, 3, 4, 5, 6, 7, 8, 9,
Transform the String
s into Integer
s first.
List ints = new ArrayList<>();
for (String s : strings)
ints.add(Integer.parseInt(s));
Collections.sort(ints);
If you don't require duplicate values, you can use a SortedSet
, which maintains the order automatically:
SortedSet ints = new TreeSet<>();
for (String s : strings)
ints.add(Integer.parseInt(s));
// all done!