You cannot actually "sort" by a specific index of an array within a document in MongoDB.
ct
If you really must do this then you need the aggregation framework to "extract" the element to sort on.
I know the list form is actually deprecated, so this code is just for demonstration. Acutally define your pipeline as individual variables and feed those as argument to aggregate:
BasicDBList pipeline = new BasicDBList();
list.add(new BasicDBObject("$unwind","$labels"));
list.add(new BasicDBObject("$group",
new BasicDBObject("_id","$_id")
.append("property", new BasicDBObject("$first","$property"))
.append("itemid", new BasicDBObject("$first","$itemid"))
.append("labels", new BasicDBObject("$push","$labels"))
.append("maxLabel", new BasicDBObject("$max", "$labels.value"))
));
list.add(new BasicDBObject("$sort", new BasicDBObject("maxLabel",1)));
System.out.println(pipeline);
That gives you the serialized version which is the JSON form of:
db.collection.aggregate([
{ "$unwind" : "$labels" },
{ "$group": {
"_id": "$_id",
"property": { "$first" : "$property" },
"itemid": { "$first" : "$itemid" },
"labels": { "$push" : "$labels" },
"maxLabel": { "$max" : "$labels.value"}
}},
{ "$sort" : { "maxLabel" : 1} }
])
Better applied in your code as:
collection.aggregate(unwind,group,sort);
Where those are individually declared.