javascript - Sort Data In Node.JS - Stack Overflow

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

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?

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?

Share Improve this question edited Mar 14, 2017 at 15:43 Matt Goodrich 5,0955 gold badges27 silver badges39 bronze badges asked Mar 14, 2017 at 15:21 Yosapat TambunanYosapat Tambunan 211 gold badge1 silver badge4 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

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 pareFunction (a, b) {
    // Here a and b are the two values in any particular instance of parison
    // 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(pareFunction)
//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 ments 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 pareFunction (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(pareFunction)

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

Generally a number should be returned from the callback:

If pareFunction(a, b) is less than 0, sort a to an index lower than b, i.e. a es first.

If pareFunction(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 (e.g. Mozilla versions dating back to at least 2003) respect this.

If pareFunction(a, b) is greater than 0, sort b to an index lower than a, i.e. b es first.

if You Use mongoo db in your Project Can Use .sort({create_at:-1}) for example :

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744259673a4565568.html

相关推荐

  • javascript - Sort Data In Node.JS - Stack Overflow

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

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信