SortBy:
var collection = [' John ', ' petteri ', ' Antti ', ' Joonas ', ' Zhentian ']; var sorted = _.sortby (collection); // [' Antti ', ' John ', ' Joonas ', ' petteri ', ' Zhentian ']
var collection = [' John ', ' petteri ', ' Antti ', ' Joonas ', ' Zhentian ']; var sorted = _.sortby (collection). reverse (); // [' Zhentian ', ' petteri ', ' Joonas ', ' John ', ' Antti ']
var = [ collection, Name: "Zach"}, 33,name: "Beth"}, 8,name: " Yolanda "}, 57,name:" Chris "}, 80,name:" Abe "}]; var sorted = _.sortby (collection, "Age"); /* [{age:8, Name: ' Yolanda '}, {age:33, Name: ' Beth '}, {age:57, Name: ' Chris '}, {age:80, Name: ' Abe '}, {age:90, Name: ' Zach '}]* /
Sortedindex:
varCollection =[{Age:*, Name: "Zach"}, {age:33,name: "Beth"}, {age:8,name: "Yolanda"}, {age:57,name: "Chris"}, {age:80,name: "Abe"}];varNewguy = {age:26, name: "Wan"};varSortedcollection = _.sortby (collection, "Age"); Console.log (sortedcollection);//want to insert-a new guy, first find-his-position in the arrayvarindex = _.sortedindex (sortedcollection, Newguy, "age"); Console.log (index); //1//insert into the array.Sortedcollection.splice (Index, 0, Newguy);/*[{age:8, Name: ' Yolanda '}, {age:26, Name: ' Wan '}, {age:33, Name: ' Beth '}, {age:57, Name: ' Chris '}, { AGE:80, Name: ' Abe '}, {age:90, Name: ' Zach '}]*/
[Javascript + Lodash] SortBy and Sortedindex