简体   繁体   中英

Sort Data In Node.JS

I am using Node.JS and MongoDB. I've created a report with CSV file and this is my code,

function buildListCsvContent() {
   var array = [];
   var counter = 0;
   for (var i = 0; i < vm.student.length; i++) {
      var row = {
         No: counter + 1
         , Registration_Number: "'" + student.registrationNumber.toString()
         , Name: student.firstName + ' ' + student.lastName
      }
      array.push(row);
      counter++
   }
}


var args = {
   data: array
};

downloadCSV(args);

How can i sort this report by registrationNumber?

Here's one method:

for (var i = 0; i < vm.student.length; i++) {
    var row = {
        No: counter + 1
        , Registration_Number: "'" + student.registrationNumber.toString()
        , Name: student.firstName + ' ' + student.lastName
        , reg_no: student.registrationNumber   // Added this key to use in sorting
    }
    array.push(row);
    counter++
 }

Use the array.sort method:

function compareFunction (a, b) {
    // Here a and b are the two values in any particular instance of comparison
    // a is put before b if return condition is true, not swapped if condition is false
    // return <condition>

    return a.reg_no > b.reg_no // For ascending
    return a.reg_no < b.reg_no // For descending
}

array.sort(compareFunction)
//array is sorted at this point

I suggest you play around with the return condition to get a good hang of the working.

You can sort the array using the sort function.

Firstly, truncate the number to remove the ' and then convert the value into an Number using the Number Object.

Following is a code with sample data, i have taken a shorter version of the Array to demonstrate.

 var array = []; array.push({Registration_Number: "'1"}, {Registration_Number: "'11"}, {Registration_Number: "'12"}, {Registration_Number: "'-5"}, {Registration_Number: "'8"} ); array.sort((x,y) => { var xInt = new Number(x.Registration_Number.substring(1,x.length)); var yInt = new Number(y.Registration_Number.substring(1,x.length)); return xInt - yInt; }); console.log(array);

-- Sorry, but I can't write comments to the posts ¯_(ツ)_/¯ --

To clarify what surajck wrote. According to mdn docs if you want to sort numbers you should subtract one from another, ie

function compareFunction (a, b)
     // you could wrap values in `Number()` to convert them to numbers, but 
     // in case of subtraction it's handled automatically
     return a.reg_no - b.reg_no // For ascending
     return b.reg_no - a.reg_no // For descending
}

array.sort(compareFunction)

If you're returning boolean you might end with inconsistent sorting.

Generally a number should be returned from the callback:

If compareFunction(a, b) is less than 0, sort a to an index lower than b, ie a comes first.

If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (eg Mozilla versions dating back to at least 2003) respect this.

If compareFunction(a, b) is greater than 0, sort b to an index lower than a, ie b comes first.

如果您在项目中使用 mongoo db 可以使用.sort({create_at:-1})例如:

        Model.find().sort({create_at:-1}).exec(function(err, user) {})

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