简体   繁体   中英

Sorting multiple objects in JavaScript

How do i sort my objects that contains numbers and other information?

I have 2 objects. The likes that is also link with the information object. Likesobject contains random number while the viewname will be related to likesobject. Next thing is, viewname will be added more and more if there are new names coming in. So it wont just stop at d: link d

Example

var viewnames = {
  'a': 'link a',
  'b': 'link b',
  'c': 'link c',
  'd': 'link d'
};

var Likeobject = {
  'a': 6,
  'b': 1,
  'c': 9,
  'd': 8
};

Example,

name1 -> 2 likes
name2 -> 5 likes
name3 -> 10 likes
name4 -> 8 likes
name5 -> 12 likes`

After sorting out from highest to lowest,

name5 -> 12 likes
name3 -> 10 likes
name4 -> 8 likes
name2 -> 5 likes
name1 -> 2 likes
var obj1 = [{numLikes: likesobject},{viewerName: viewnames}];

I am not sure if the way I code my object is the right way. Do correct me if I'm wrong. Hope to get solution to my question.

Use the array.sort

var myArray = [
    {"numLikes": 2, "viewerName": "name1"},
    {"numLikes": 5, "viewerName": "name2"},
    {"numLikes": 10, "viewerName": "name3"},
    {"numLikes": 8, "viewerName": "name4"},
    {"numLikes": 12, "viewerName": "name5"}
];
printArray();
sortArray();
printArray();
myArray.push({"numLikes" : 17, "viewerName" : "name6"}); // add another object to the array
myArray.push({"numLikes" : 1, "viewerName" : "name7"}); // add another object to the array
printArray();
sortArray();
printArray();
changeNumLikes("name1", 7);
printArray();
sortArray();
printArray();

function sortArray() {
    myArray.sort(function(a, b){
        return a.numLikes-b.numLikes;
    });
}

function printArray() {
    for (let i = 0; i < myArray.length; i++) {
        document.write("<span>" + myArray[i].viewerName + " : " + myArray[i].numLikes + "</span><br/>");
    }
    document.write("<br/>");
}

function changeNumLikes(viewerName, numLikes) {
    let object;
    for (let i = 0; i < myArray.length; i++) {
        if (myArray[i].viewerName === viewerName) {
            object = myArray[i];
            break;
        }
    }
    if (object && object.numLikes) {
        object.numLikes = numLikes;
    }
}

How do i sort my objects that contains numbers and other information?

You don't sort an object.

Why, because an object is an unordered collection of properties.

I guess you want to sort an array of objects by a objects property, so see https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/sort you can give it a compare function.

Real object example would be useful.

You could get the keys in an array for sorting, sort by likes descending and use the keys array for generating a list of the sorted result.

This solution uses Array#sort .

 var viewNames = { a: 'link a', b: 'link b', c: 'link c', d: 'link d' }, likes = { a: 6, b: 1, c: 9, d: 8 }, keys = Object .keys(likes) .sort((a, b) => likes[b] - likes[a]); keys.forEach(key => console.log(key, viewNames[key], likes[key])); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM