0

I am trying to do sort id

Modal:

module.exports = { autoPK: false, attributes: { id: { type: 'integer', autoIncrement:true, primaryKey: true }, } }

Query:

mymodal.find().sort({id: 'asc'}).exec(function (err, res) {
    console.log(res)
});

Data:

[ { id: '2', },{ id: '1'},{ id: '11' } ]

Actual:

[ { id: '1', },{ id: '11'},{ id: '2' } ]

**Expected:

[ { id: '1', },{ id: '2'},{ id: '11' } ]**

Can anybody help me out. Please..

Sorting on string working, but on number(interger).

Is there any with my query or issue with sails waterline criteria sort

Max
  • 71
  • 9
  • when you watch in your database, are you sure if "id" is integer type, your migrate in local.js is equal to "alter"?. you should copy your JSON result, only find(). – jhonny lopez Dec 02 '15 at 22:11
  • Yes.. I am sure in my model id -> type: 'integer', autoIncrement: true, primaryKey: true. Have you tried sorting by id where, id is numeric. using sails-redis. Colud please help me? – Max Dec 07 '15 at 12:01

3 Answers3

1

First you should do query find and later sort()

Modal.find()
.sort({id: 'asc'})
.exec(function(err, res) {
  console.log(res)
});
jhonny lopez
  • 325
  • 1
  • 2
  • 9
  • Sorry for the mistake, i have corrected my query on post. Any help – Max Dec 02 '15 at 20:59
  • Need is sails waterline sort by number not by string. Do anybody tried or faced this issue, any alternative,? – Max Dec 02 '15 at 21:01
0

I went through the sails-redis adapter index and schema, few things are missing. while parsing the input data like '11' the method not worried about the data type. Data like { id: 1 } and { id: '1'} is consider to be different even though in the model type: 'integer specified.

sails-redis/lib/database/schema.js

changes code: Schema.prototype.parse . . case 'integer': values[key] = parseInt(values[key]); break; case 'float': values[key] = parseFloat(values[key]); break; } } . . changes working for me. Make sure the values[key] is not null

Max
  • 71
  • 9
0

The documentation isn't great on this, but it looks like you might need to use binary notation if you pass an object to sort(). Easier to just pass a string:

Model.find().sort('id asc').exec(function (err, res) {
  console.log(res)
});
carpiediem
  • 1,918
  • 22
  • 41