Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a query by a ListProperty (NDB)

How do i sort a query by a ListProperty*?

the Model:

class Chapter(ndb.Model):
    title = ndb.StringProperty(required=True)
    version = ndb.IntegerProperty(repeated=True)

the 'version' stores values like:

1.1 -> [1,1]
1 -> [1]
2.1.1.1.1 -> [2,1,1,1,1]
1.2 -> [1,2]
2.1.2 -> [2,1,2]

I want to order it like:

1
1.1
1.2
2.1.1.1.1
2.1.2

*Im using NDB so, ListProperty = ndb.IntegerProperty(repeated=True)

like image 680
mFontoura Avatar asked Dec 06 '25 08:12

mFontoura


2 Answers

That's not how listproperties work, unfortunately. For an ascending-order query, the value used will be the smallest in the list. You'll have to store the values differently (as a string, for example) to do what you're asking.

like image 182
Greg Avatar answered Dec 07 '25 21:12

Greg


OMG I finally did it.

newChaps = sorted(chaps, key=lambda obj: obj.version)

So simple... and yet so much time to find it...

like image 21
mFontoura Avatar answered Dec 07 '25 20:12

mFontoura



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!