import {useState} from 'react';
import data from './data';
const split = data.map((l, id) => ({"words": l.split(" "), id})),
maxWords = split.reduce((c, w) => Math.max(c, w.words.length), 0),
stringSort = new Intl.Collator().compare,
Row = (params: {words: string[]}) => (
{params.words.map((word: string, n: number) => ({word} | ))}
{Array.from({"length": maxWords - params.words.length}, () => (- | ))}
),
SortWord = () => {
const [sortNum, setSortNum] = useState(-1),
[reverse, setReverse] = useState(false);
return (
{Array.from({"length": maxWords}, (_, n) => ( {
if (sortNum === n) {
setReverse(!reverse);
} else {
setSortNum(n);
setReverse(false);
}
}}>Word {n+1} | ))}
{split.slice().sort(sortNum === -1 ? () => 0 : (a, b) => stringSort(a.words[sortNum] ?? "", b.words[sortNum] ?? "") * (reverse ? -1 : 1)).map(({words, id}) => (
))}
);
};
export default SortWord;