[SERVER-7401] sort() sorts wrong when sorted string contains numbers Created: 18/Oct/12  Updated: 15/Feb/13  Resolved: 18/Oct/12

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 2.2.0
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Manuel Baesler Assignee: Unassigned
Resolution: Done Votes: 0
Labels: find, sort
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

OS X 10.8.2 (12C60) 64bit
db version v2.2.0, pdfile version 4.5
Thu Oct 18 14:40:04 git version: f5e83eae9cfbec7fb7a071321928f00d1b0c5207


Operating System: ALL
Participants:

 Description   

Is this normal behavior or a bug?

> db.test.insert({a:1})
> db.test.insert({a:10})
> db.test.insert({a:9})
> db.test.insert({a:4})
> db.test.insert({a:4})
> db.test.insert({a:50}) 
> 
> db.test.find().sort({a:1})
{ "_id" : ObjectId("507ff0d917258e582f785be1"), "a" : 1 }
{ "_id" : ObjectId("507ff0e117258e582f785be4"), "a" : 4 }
{ "_id" : ObjectId("507ff0e217258e582f785be5"), "a" : 4 }
{ "_id" : ObjectId("507ff0df17258e582f785be3"), "a" : 9 }
{ "_id" : ObjectId("507ff0dd17258e582f785be2"), "a" : 10 }
{ "_id" : ObjectId("507ff0e517258e582f785be6"), "a" : 50 }
> db.test.remove()
> 
> db.test.insert({a:'a1'})
> db.test.insert({a:'a10'})
> db.test.insert({a:'a20'})
> db.test.insert({a:'a4'})
> db.test.insert({a:'a2'})
> db.test.insert({a:'a4'})
> db.test.insert({a:'a9'})
> 
> db.test.find().sort({a:1})
{ "_id" : ObjectId("507ff10117258e582f785be7"), "a" : "a1" }
{ "_id" : ObjectId("507ff10317258e582f785be8"), "a" : "a10" }
{ "_id" : ObjectId("507ff10b17258e582f785beb"), "a" : "a2" }
{ "_id" : ObjectId("507ff10517258e582f785be9"), "a" : "a20" }
{ "_id" : ObjectId("507ff10817258e582f785bea"), "a" : "a4" }
{ "_id" : ObjectId("507ff11317258e582f785bec"), "a" : "a4" }
{ "_id" : ObjectId("507ff11517258e582f785bed"), "a" : "a9" }

I thought it will sort a1, a2, a4,... , a10, a20



 Comments   
Comment by Scott Hernandez (Inactive) [ 18/Oct/12 ]

This is how strings sort, and normal. See http://en.wikipedia.org/wiki/Lexicographical_order

Generated at Thu Feb 08 03:14:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.