22Sorts Builders
33==============
44
5- .. default-domain:: mongodb
5+ .. facet::
6+ :name: genre
7+ :values: reference
8+
9+ .. meta::
10+ :keywords: code example, sorting
611
712.. contents:: On this page
813 :local:
@@ -43,12 +48,12 @@ contains the following documents:
4348
4449.. code-block:: json
4550
46- {"_id": 1, "letter ": "c ", "food ": "coffee with milk"}
47- {"_id": 3 , "letter ": "a ", "food ": "maple syrup"}
48- {"_id": 4 , "letter ": "b ", "food ": "coffee with sugar"}
49- {"_id": 5 , "letter ": "a ", "food ": "milk and cookies"}
50- {"_id": 2 , "letter ": "a ", "food ": "donuts and coffee"}
51- {"_id": 6, "letter ": "c ", "food ": "maple donut" }
51+ { "_id": 1, "date ": "2022-01-03 ", "orderTotal ": 17.86, "description": "1/2 lb cream cheese and 1 dozen bagels" },
52+ { "_id": 2 , "date ": "2022-01-11 ", "orderTotal ": 83.87, "description": "two medium strawberry birthday cakes" },
53+ { "_id": 3 , "date ": "2022-01-11 ", "orderTotal ": 19.49, "description": "1 dozen strawberry cupcakes" },
54+ { "_id": 4 , "date ": "2022-01-15 ", "orderTotal ": 43.62, "description": "2 chicken lunches and a diet coke" },
55+ { "_id": 5 , "date ": "2022-01-23 ", "orderTotal ": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" },
56+ { "_id": 6, "date ": "2022-01-23 ", "orderTotal ": 60.31, "description": "one large strawberry and chocolate cake" }
5257
5358The Sorts Class
5459---------------
@@ -92,9 +97,9 @@ The output of the preceding example should look something like this:
9297.. code-block:: json
9398 :copyable: false
9499
95- {"_id": 1, "letter ": "c ", "food ": "coffee with milk" }
96- {"_id": 2, "letter ": "a ", "food ": "donuts and coffee" }
97- {"_id": 3, "letter ": "a ", "food ": "maple syrup" }
100+ { "_id": 1, "date ": "2022-01-03 ", "orderTotal ": 17.86, "description": "1/2 lb cream cheese and 1 dozen bagels" }
101+ { "_id": 2, "date ": "2022-01-11 ", "orderTotal ": 83.87, "description": "two medium strawberry birthday cakes" }
102+ { "_id": 3, "date ": "2022-01-11 ", "orderTotal ": 19.49, "description": "1 dozen strawberry cupcakes" }
98103 ...
99104
100105Descending
@@ -120,9 +125,9 @@ The preceding example should output something like this:
120125.. code-block:: json
121126 :copyable: false
122127
123- {"_id": 6, "letter ": "c ", "food ": "maple donut" }
124- {"_id": 5, "letter ": "a ", "food ": "milk and cookies" }
125- {"_id": 4, "letter ": "b ", "food ": "coffee with sugar" }
128+ { "_id": 6, "date ": "2022-01-23 ", "orderTotal ": 60.31, "description": "one large strawberry and chocolate cake" }
129+ { "_id": 5, "date ": "2022-01-23 ", "orderTotal ": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" }
130+ { "_id": 4, "date ": "2022-01-15 ", "orderTotal ": 43.62, "description": "2 chicken lunches and a diet coke" }
126131 ...
127132
128133Combining Sort Criteria
@@ -146,20 +151,20 @@ on the ``letter`` field, and in the event of a tie, ascending order on the
146151
147152 // <MongoCollection setup code here>
148153
149- Bson orderBySort = orderBy(descending("letter "), ascending("_id "));
154+ Bson orderBySort = orderBy(descending("date "), ascending("orderTotal "));
150155 collection.find().sort(orderBySort);
151156
152157The output of the preceding example should look something like this:
153158
154159.. code-block:: json
155160 :copyable: false
156161
157- {"_id": 1 , "letter ": "c ", "food ": "coffee with milk" }
158- {"_id": 6, "letter ": "c ", "food ": "maple donut" }
159- {"_id": 4, "letter ": "b ", "food ": "coffee with sugar" }
160- {"_id": 2 , "letter ": "a ", "food ": "donuts and coffee" }
161- {"_id": 3 , "letter ": "a ", "food ": "maple syrup" }
162- {"_id": 5 , "letter ": "a ", "food ": "milk and cookies" }
162+ { "_id": 5 , "date ": "2022-01-23 ", "orderTotal ": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" }
163+ { "_id": 6, "date ": "2022-01-23 ", "orderTotal ": 60.31, "description": "one large strawberry and chocolate cake" }
164+ { "_id": 4, "date ": "2022-01-15 ", "orderTotal ": 43.62, "description": "2 chicken lunches and a diet coke" }
165+ { "_id": 3 , "date ": "2022-01-11 ", "orderTotal ": 19.49, "description": "1 dozen strawberry cupcakes" }
166+ { "_id": 2 , "date ": "2022-01-11 ", "orderTotal ": 83.87, "description": "two medium strawberry birthday cakes" }
167+ { "_id": 1 , "date ": "2022-01-03 ", "orderTotal ": 17.86, "description": "1/2 lb cream cheese and 1 dozen bagels" }
163168
164169Text Score
165170----------
0 commit comments