sort-object-array-by-property

node v16.18.0
version: 2.0.19
endpointsharetweet
I) Defining an array of arrays for testing function 'sortObjectArrByProps':
// import package commonJs / destructuring var { sortObjectArrByProps } = require("@nighly/sort-object-array-by-property"); // an array of objects {}[] or an array of arrays [][] for testing: // * an array of arrays: the properties are called indices, and are numbers: var list = [ [ 1, 65, 74, 25 ], [ 2, 65, 36, 20 ], [ 1, 65, 74, 20 ], [ 1, 65, 32, 15], [ 2, 65, 36, 40 ], [ 1, 65, 32, 25 ] ]; // unordered list list.forEach((item) => console.log(item));
I) 1. sortObjectArrByProps( list, [ 0, 1, 2, 3 ] )
// returns a sorted version of 'list' according to indices 0, 1, 2 and 3, // in this order of priority, leaving the variable 'list' unaltered: const sorted_list_1 = sortObjectArrByProps(list, [ 0, 1, 2, 3 ]); // all indices [0, 1, 2, 3] sorted_list_1.forEach((item) => console.log(item));
I) 2. sortObjectArrByProps( list, [ 3, 2 ] )
var sorted_list_2 = sortObjectArrByProps(list, [ 3, 2 ]); // fourth and third indices [3, 2] sorted_list_2.forEach((item) => console.log(item));
I) 3. sortObjectArrByProps( list, [ 1, 0 ] )
var sorted_list_3 = sortObjectArrByProps(list, [ 1, 0 ]); // second and first indices [1, 0] sorted_list_3.forEach((item) => console.log(item));
I) 4. sortObjectArrByProps( list, 2 )
var sorted_list_4 = sortObjectArrByProps(list, 2); // third index 2 sorted_list_4.forEach((item) => console.log(item))
I) 5. sortObjectArrByProps( list, [ 0, 1, 2, 3 ], 'r' )
var sorted_list_1_reversed = sortObjectArrByProps(list, [ 0, 1, 2, 3 ], 'r'); // all indices [0, 1, 2, 3], reverse order 'r' sorted_list_1_reversed.forEach((item) => console.log(item));
II) Defining a more complex array of arrays for testing function 'sortObjectArrByProps':
// * an array with an intenger and an object containing another object // some values are missing var list2 = [ [ 2, {content: { empty: true }} ], [ 2, {content: { empty: true, load: -1 }} ], [ 1 ], [ 3, {content: { empty: false, load: 12 }} ], [ 3, {content: { empty: true, load: 0 }} ], [ 1, {content: { empty: false, load: 8 }} ] ]; list2.forEach((item) => console.log(item));
II) 6. sortObjectArrByProps( list2, [ "1.content.load", 0 ] )
var sorted_list_6 = sortObjectArrByProps(list2, "1.content.load" ); // second index > content > load sorted_list_6.forEach((item) => console.log(item)); // the last objects form a third group [1) true, 2) false, 3) undefined] since they lack the single property used to sort the array
II) 7. sortObjectArrByProps( list2, [ 0, "1.content.empty" ] )
var sorted_list_7 = sortObjectArrByProps(list2, [ 0, "1.content.empty", "1.content.load" ] ); // [ first index, second index > content > empty, second index > content > load ] sorted_list_7.forEach((item) => console.log(item)); // in this case all elements have the 0 index; the second and the fourth element go down because they lack one or more properties used to sort the array, but still float above elements whose value of index 0 is more than their own
II) 8. sortObjectArrByProps( list2, [ "1.content.load", 0 ] );
var sorted_list_8 = sortObjectArrByProps(list2, [ "1.content.load", 0 ] ); // [ second index > content > load, first index ] // any item without "1.content.load" will go down, then will be ordered by first index 0 sorted_list_8.forEach((item) => console.log(item));
II) 9. sortObjectArrByProps( list2, [ "1.content.load", 0, "-R" ] );
var sorted_list_8 = sortObjectArrByProps(list2, [ "1.content.load", 0 ], "-R" ); // [ second index > content > load, first index ] // the first property is sorted in standard ascending order, and the second one is sorted in reversed descending order sorted_list_8.forEach((item) => console.log(item));
II) 10. sortObjectArrByProps( list2, [ "1.content.load", 0, "rr" ] );
var sorted_list_8 = sortObjectArrByProps(list2, [ "1.content.load", "1.content.empty", 0 ], "rr" ); // [ second index > content > load, first index ] // the first and the second properties are sorted in reverse descending order, and the third one is sorted in standard ascending order sorted_list_8.forEach((item) => console.log(item));
Loading…

no comments

    sign in to comment