W jaki sposób posortować wszystkie wyrazy alfabetycznie według wzkazanego paternu?

0

Jak mogę posortować wszystkie wyrazy według mojego własnego alfabetu?

Zrobiłem już, że sortuje ale patrzy tylko na pierwszą literę wyrazu, ja potrzebuje aby patrzało na każdą literę wyrazu.

przykład zwykłego alfabetu:

cbc
cac
bcc
ccc

sortuje na:

bcc
cac
cbc
ccc
        function sortInput() {
            var str = document.getElementById("input").value;
            var output = document.getElementById('output');

            var sort = "DTCXVZOYALGIUMBWRPSQNKJHFE".toLowerCase();

            str = str.split(' ').filter(inString).sort(sortByString).join(' ');
            
            console.log(str);
            output.value = str;


            function sortByString (a, b) {
                return sort.indexOf(a.charAt(0).toLowerCase()) > sort.indexOf(b.charAt(0).toLowerCase())
                    ? 1
                    : -1
            }

            function inString(s) {
                return sort.indexOf(s.charAt(0).toLowerCase()) !== -1
            }
        }
}
1

Wersja napisana na kolanie:

const alphabet = ['X', 'Y', 'Z', 'A', 'C', 'D', /* ... */];

function compareStrings(a, b) {
  const length = Math.min(a.length(), b.length());

  for (let i = 0; i < length; i += 1) {
    const cmp = alphabet.indexOf(a[i]) - alphabet.indexOf(b[i]);

    if (cmp != 0) {
      return cmp;
    }
  }

  return 0;
}
0
Patryk27 napisał(a):

Coś nie działa..


        function sortInput() {

            var str = document.getElementById("input").value;
            var output = document.getElementById('output');

            const alphabet = ['D', 'T', 'C', 'X', 'V', 'Z', 'O', 'Y', 'A', 'L', 'G', 'I', 'U', 'M', 'B', 'W', 'R', 'P', 'S', 'Q', 'N', 'K', 'J', 'H', 'F', 'E'];

            str = str.split(' ').sort(compareStrings).join(' ');
            
            console.log(str);
            output.value = str;

            function compareStrings(a, b) {
            const length = Math.min(a.length(), b.length());
            
            for (let i = 0; i < length; i += 1) {
                const cmp = alphabet.indexOf(a[i]) - alphabet.indexOf(b[i]);
            
                if (cmp != 0) {
                return cmp;
                }
            }
            
            return 0;
            }
}
0

Połączyłem trochę swojego kodu z główną koncepcją @Patryk27 i powstało coś takiego (w pierwszej wersji za bardzo chciałem przekombinować :P)

stupid & simple, not clever

const SORT_DIRECTION = {
    ASC: 0,
    DESC: 1
};

const sort = order => (input, direction = SORT_DIRECTION.ASC) => {
    const compare = (lhs, rhs, direction) =>
        direction === SORT_DIRECTION.ASC
            ? (lhs > rhs) - (lhs < rhs)
            : (rhs > lhs) - (rhs < lhs);

    return [...input].sort((a, b) => {
        const length = Math.min(a.length, b.length);

        for (let i = 0; i < length; i++) {
            const cmp = compare(order.indexOf(a[i]), order.indexOf(b[i]), direction);

            if (cmp !== 0) {
                return cmp;
            }
        }

        return compare(a.length, b.length, direction);
    });
};

const order = ['D', 'T', 'C', 'X', 'V', 'Z', 'O', 'Y', 'A', 'L', 'G', 'I', 'U', 'M', 'B', 'W', 'R', 'P', 'S', 'Q', 'N', 'K', 'J', 'H', 'F', 'E'];
const sortByCustomAlphabet = sort(order);

const data = ['C', 'CC', 'CCC', 'T', 'TT', 'TTT', 'X', 'XX', 'XXX'];
const sortedAsc = sortByCustomAlphabet(data);
const sortedDesc = sortByCustomAlphabet(data, SORT_DIRECTION.DESC);

console.log(sortedAsc);
console.log(sortedDesc);
console.log(sortedAsc);
console.log(sortedDesc.reverse());

1 użytkowników online, w tym zalogowanych: 0, gości: 1