ts multi attribute sorting

Now there is an array of students:

enum Sex {
    man,
    woman
}

interface Student {
    Name: string,
    Age: number,
    Sex: Sex
}

const Students: Student[] = [
    { Name: 'lux', Age: 18, Sex: Sex.man },
    { Name: 'hey', Age: 21, Sex: Sex.man },
    { Name: 'haha', Age: 23, Sex: Sex.woman },
    { Name: 'yo', Age: 19, Sex: Sex.woman }
]

You need to sort by age (or other attributes).

In the last note, Ruan Yifeng's Quick sort:

var arr = [6,2,5,1,3];

function quickSort(arr:Number[]):Number[]{
    if(arr.length <= 1){
        return arr;
    }
    var pivotIndex = Math.floor(arr.length/2);
    var pivot = arr.splice(pivotIndex,1)[0];
    var left = [] as Number[];
    var right = [] as Number[];

    for(var i =0;i<arr.length;i++){
        if (arr[i] < pivot) {
            left.push(arr[i]);
        } else {
            right.push(arr[i]);
        }
    }
    return quickSort(left).concat([pivot], quickSort(right));
}
console.log(quickSort(arr));

Then, the sorting of student array is also based on this fast sorting method.
First, we need to add a parameter to the function signature to pass the properties of the student array.

function orderby<Titem, Tvalue>(arr: Titem[], selector: (i: Titem) => Tvalue): Titem[] 

(it uses generic paradigm Concept)
The parameter arr represents the student array we need to pass in, and the selector represents a specific attribute in the student array.
The next section is the source code of quick sorting:

if (arr.length <= 1) {
        return arr;
    }//If the item of the array is less than or equal to 1, it is OK to return the array directly
const pivotIndex = Math.floor(arr.length / 2);//Index in the middle
const pivot = arr.splice(pivotIndex, 1)[0];//Students in the middle
const left = [] as Titem[];
const right = [] as Titem[];

The next paragraph is used to judge the sorting.

for (const i of arr) {
    if (selector(i) < selector(pivot)) {
        left.push(i);
    } else {
        right.push(i);
    }
}
return orderby(left, selector).concat([pivot], orderby(right, selector));

Method called:

const StudentOrderbyAge = orderby(Students, i => i.Age);
console.log(StudentOrderbyAge);

Source code:

function orderby<Titem, Tvalue>(arr: Titem[], selector: (i: Titem) => Tvalue): Titem[] {
    if (arr.length <= 1) {
        return arr;
    }
    const pivotIndex = Math.floor(arr.length / 2);
    const pivot = arr.splice(pivotIndex, 1)[0];
    const left = [] as Titem[];
    const right = [] as Titem[];

    for (const i of arr) {
        if (selector(i) < selector(pivot)) {
            left.push(i);
        } else {
            right.push(i);
        }
    }
    return orderby(left, selector).concat([pivot], orderby(right, selector));
}

const StudentOrderbyAge = orderby(Students, i => i.Age);
console.log(StudentOrderbyAge);

JS source code:

function orderby(arr, selector) {
    if (arr.length <= 1) {
        return arr;
    }
    const pivotIndex = Math.floor(arr.length / 2);
    const pivot = arr.splice(pivotIndex, 1)[0];
    const left = [];
    const right = [];

    for (const i of arr) {
        if (selector(i) < selector(pivot)) {
            left.push(i);
        } else {
            right.push(i);
        }
    }
    return orderby(left, selector).concat([pivot], orderby(right, selector));
}

const StudentOrderbyAge = orderby(Students, i => i.Age);
console.log(StudentOrderbyAge);

Tags: Attribute less

Posted on Thu, 13 Feb 2020 15:59:52 -0500 by provision