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 badges4 Answers
Reset to default 3Here'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
评论列表(0条)