N
N
Nubbin2018-11-27 19:39:44
JavaScript
Nubbin, 2018-11-27 19:39:44

Typescript how to sort by name?

Good evening, guys, how to sort not by key but by name

var stringArray: string[] = ['sd', 'Stas', 'Dima', 'AC'];
var sortedArray: string[] = stringArray.sort((n1,n2) => {
    if (n1 > n2) {
        return 1;
    }

    if (n1 < n2) {
        return -1;
    }

    return 0;
});

It is necessary that "Dima" was always on top. in 0 position

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavlo Ponomarenko, 2018-11-27
@Nubbin

You need to write a coolness function and sort not just by name, but by the value of the coolness function from the name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question