简体   繁体   English

在 Node.JS 中对数据进行排序

[英]Sort Data In Node.JS

I am using Node.JS and MongoDB.我正在使用 Node.JS 和 MongoDB。 I've created a report with CSV file and this is my code,我用 CSV 文件创建了一个报告,这是我的代码,

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:使用array.sort方法:

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.您可以使用sort函数对数组进行sort

Firstly, truncate the number to remove the ' and then convert the value into an Number using the Number Object.首先,截断数字以删除' ,然后使用Number对象将值转换为Number

Following is a code with sample data, i have taken a shorter version of the Array to demonstrate.以下是带有示例数据的代码,我使用了一个较短版本的 Array 来演示。

 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.澄清 surajck 写了什么。 According to mdn docs if you want to sort numbers you should subtract one from another, ie根据 mdn 文档,如果你想对数字进行排序,你应该从另一个中减去一个,即

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.如果 compareFunction(a, b) 小于 0,则将 a 排序到低于 b 的索引,即 a 在前。

If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.如果 compareFunction(a, b) 返回 0,则使 a 和 b 彼此保持不变,但相对于所有不同元素进行排序。 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.注意:ECMAscript 标准不保证这种行为,因此并非所有浏览器(例如至少可以追溯到 2003 年的 Mozilla 版本)都尊重这一点。

If compareFunction(a, b) is greater than 0, sort b to an index lower than a, ie b comes first.如果 compareFunction(a, b) 大于 0,则将 b 排序到低于 a 的索引,即 b 在前。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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