Skip to content

Instantly share code, notes, and snippets.

@rmg007
Created October 16, 2022 15:15
Show Gist options
  • Save rmg007/b3074e2fc3b6b1fa26d31a39e52fc5c8 to your computer and use it in GitHub Desktop.
Save rmg007/b3074e2fc3b6b1fa26d31a39e52fc5c8 to your computer and use it in GitHub Desktop.
Sort String By Second Char
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class SortStringBySecondChar {
public static void main(String[] args) {
List<String> list = new ArrayList<>(List.of("Byyyyyyyyyyyy", "Dww", "Cxxxxxx", "az"));
Comparator<String> sortBySecondCharComparator = (s1, s2) -> Integer.compare(s2.charAt(1), s1.charAt(1));
list.sort(sortBySecondCharComparator);
System.out.println(list);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment