I have map looks like this
key: + something
value: something
So it have a big amount of strings starting with + or -, and I need to sort it alphabetically.
For example:
{
host: hexlet.io,
+ timeout: 20,
+ verbose: true,
- follow: false,
- proxy: 123.234.53.22,
- timeout: 50
}
And it must be sorted to this:
{
- follow: false,
host: hexlet.io,
- proxy: 123.234.53.22,
- timeout: 50,
+ timeout: 20,
+ verbose: true
}
How can I sort it skipping "+" or "-"?
UPD:
Thx so much! But i missed out that my boileplate must be kinda of this: (it contains 2 whitespace before minus and 1 whitespace after it)
{
- follow: false
host: hexlet.io
- proxy: 123.234.53.22
- timeout: 50
+ timeout: 20
+ verbose: true
}
So if i make s -> s.substring(2) it doesn`t sort it alphabetically, or if i use 3-4, my "timeout" string crashes, i have "+ timeout: 50" instead of "-timeout: 50" and "+ timeout: 20". How can i fix it?
We can create a
TreeMapwith a customComparatorand then useputAllto add all elements from an existing map to the sorted implementation.According to the example, custom comparator needs to include two parts:
" + "and put it to the end, assuming that strings starting with" - "or with empty prefix" "should be treated the sameFor example:
Output (sorted in the requested order):