Given that there is a lexicographically-sorted list of UTF-8 string s1
, s2
, s3
, ... of unknown length, is it possible to invert each string value, such that when the list is sorted lexicographically again using the inverted value, the reverse order is now produced?
function invert(s) {
// TODO: what's here?
return s;
}
const sample = ['', ' ', 'a', 'A', '@', '한','자', '한자', '자한'];
const original = [...sample].sort((a, b) => {
return a.localeCompare(b);
});
const inverted = [...sample].sort((a, b) => {
return invert(a).localeCompare(invert(b));
});
// Both should print the same.
console.log('original', original);
console.log('inverted.reverse', inverted.reverse());