Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple sorting with ascending and descending in Parse Cloud

I have the following Result Class in Parse:

score <Number>  |  miliseconds <Number>
50              |  500
100             |  10000
100             |  20000
50              |  1000
50              |  2000
100             |  99999

I'm trying to sort descending by score first and then ascending by miliseconds

First try:

var query = new Parse.Query('Result')
query.descending('score')
query.ascending('time')
return query.find()

And I get this:

score <Number>  |  miliseconds <Number>
50              |  500
50              |  1000
50              |  2000
100             |  10000
100             |  20000
100             |  99999

Second try:

var query = new Parse.Query('Result')
query.ascending('time')
query.descending('score')
return query.find()

Result:

score <Number>  |  miliseconds <Number>
100             |  99999
100             |  10000
100             |  20000
50              |  500
50              |  1000
50              |  2000

What am I expecting?

I'm expecting to have a result like this:

score <Number>  |  miliseconds <Number>
100             |  10000
100             |  20000
100             |  99999
50              |  500
50              |  1000
50              |  2000

Am I doing something wrong here?

like image 751
thangngoc89 Avatar asked Dec 30 '15 22:12

thangngoc89


1 Answers

Try this way:

var query = new Parse.Query('Result')
query.descending('score')
query.addAscending('time')
return query.find()

The thing is to add another sorting condition, not overriding pervious one.

like image 92
Juri Noga Avatar answered Sep 20 '22 07:09

Juri Noga