I have a List<String>
object that contains country names. How can I sort this list alphabetically?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- How to toggle on Order in ReactJS
- StackExchange API - Deserialize Date in JSON Respo
You can try using a method that I made.
String key
- will be the order you want and in this case in alphabetically. Just put "abc...".String list[]
- the list you want to put in order using key.int index
- set as 0, will set the offset for the key.Use the two argument for of
Collections.sort
. You will want a suitableComparator
that treats case appropriate (i.e. does lexical, not UTF16 ordering), such as that obtainable throughjava.text.Collator.getInstance
.descending alphabet:
Assuming that those are Strings, use the convenient static method
sort
…Unless you are sorting strings in an accent-free English only, you probably want to use a
Collator
. It will correctly sort diacritical marks, can ignore case and other language-specific stuff:You can set the collator strength, see the javadoc.
Here is an example for Slovak: