// First create the array of keys/values so that we can sort it:var sort_array = [];for (var key in list) {sort_array.push({key:key,value:list[key]});}
// Now sort it:sort_array.sort(function(x,y){return x.value - y.value});
// Now process that object with it:for (var i=0;i<sort_array.length;i++) {var item = list[sort_array[i].key];
// now do stuff with each item}
// use slice() to copy the array and not just make a referencevar byDate = arrayOfObjects.slice(0);byDate.sort(function(a,b) {return a.born - b.born;});console.log('by date:');console.log(byDate);
按名称排序
var byName = arrayOfObjects.slice(0);byName.sort(function(a,b) {var x = a.name.toLowerCase();var y = b.name.toLowerCase();return x < y ? -1 : x > y ? 1 : 0;});
console.log('by name:');console.log(byName);
var res = [{"s1":5},{"s2":3},{"s3":8}].sort(function(obj1,obj2){var prop1;var prop2;for(prop in obj1) {prop1=prop;}for(prop in obj2) {prop2=prop;}//the above two for loops will iterate only once because we use it to find the keyreturn obj1[prop1]-obj2[prop2];});
function sortObjectProperties(obj, sortValue){var keysSorted = Object.keys(obj).sort(function(a,b){return obj[a][sortValue]-obj[b][sortValue]});var objSorted = {};for(var i = 0; i < keysSorted.length; i++){objSorted[keysSorted[i]] = obj[keysSorted[i]];}return objSorted;}
用法:
/* sample object with unsorder properties, that we want to sort bytheir "customValue" property */
var objUnsorted = {prop1 : {customValue : 'ZZ'},prop2 : {customValue : 'AA'}}
// call the function, passing object and property with it should be sorted outvar objSorted = sortObjectProperties(objUnsorted, 'customValue');
// now console.log(objSorted) will return:{prop2 : {customValue : 'AA'},prop1 : {customValue : 'ZZ'}}
var dayObj = {"Friday":["5:00pm to 12:00am"] ,"Wednesday":["5:00pm to 11:00pm"],"Sunday":["11:00am to 11:00pm"],"Thursday":["5:00pm to 11:00pm"],"Saturday":["11:00am to 12:00am"]}
想按天数排序,
我们应该先有DaySorterMap,
var daySorterMap = {// "sunday": 0, // << if sunday is first day of week"Monday": 1,"Tuesday": 2,"Wednesday": 3,"Thursday": 4,"Friday": 5,"Saturday": 6,"Sunday": 7}
发起一个单独的Object sortedDayObj,
var sortedDayObj={};Object.keys(dayObj).sort((a,b) => daySorterMap[a] - daySorterMap[b]).forEach(value=>sortedDayObj[value]= dayObj[value])
/*** Represents an associative array of a same type.*/interface Dictionary<T> {[key: string]: T;}
/*** Sorts an object (dictionary) by value or property of value and returns* the sorted result as a Map object to preserve the sort order.*/function sort<TValue>(obj: Dictionary<TValue>,valSelector: (val: TValue) => number | string,) {const sortedEntries = Object.entries(obj).sort((a, b) =>valSelector(a[1]) > valSelector(b[1]) ? 1 :valSelector(a[1]) < valSelector(b[1]) ? -1 : 0);return new Map(sortedEntries);}
用法
var list = {"one": { height: 100, weight: 15 },"two": { height: 75, weight: 12 },"three": { height: 116, weight: 9 },"four": { height: 15, weight: 10 },};
var sortedMap = sort(list, val => val.height);
JavaScript对象中键的顺序不能保证,所以我将排序并将结果作为保留排序顺序的Map对象返回。
如果你想把它转换回Object,你可以这样做:
var sortedObj = {} as any;sortedMap.forEach((v,k) => { sortedObj[k] = v });
let list = {"you": 100,"me": 75,"foo": 116,"bar": 15};
let sorted = Object.entries(list).sort((a,b) => a[1] - b[1]);
for(let element of sorted) {console.log(element[0]+ ": " + element[1]);}
var list = {"you": 100,"me": 75,"foo": 116,"bar": 15};
let result = Object.keys(list).sort((a,b)=>list[a]>list[b]?1:-1).reduce((a,b)=> {a[b]=list[b]; return a},{});
console.log(result);
var list = {"you": 100,"me": 75,"foo": 116,"bar": 15};var sortArray = [];
// convert the list to array of key and value pairfor(let i in list){sortArray.push({key : i, value:list[i]});}
//console.log(sortArray);// sort the array using value.sortArray.sort(function(a,b){return a.value - b.value;});
//console.log(sortArray);
// now create a newList of required format.let newList={};for(let i in sortArray){newList[sortArray[i].key] = sortArray[i].value;}
console.log(newList);