Sorting

The following examples represent NQ sorting techniques. Store Pilots function is used to fill in the database.

GetSortedPilots

Select all the pilots from the database and sort descending by points. 

SimpleExamples.cs: GetSortedPilots
01public static void GetSortedPilots() 02 { 03 IObjectContainer container = Database(); 04 try 05 { 06 IList<Pilot> result = container.Query<Pilot>(delegate(Pilot pilot) 07 { 08 return true; 09 }, 10 // sort by points 11 delegate(Pilot p1, Pilot p2) 12 { 13 return p2.Points - p1.Points; 14 }); 15 ListResult(result); 16 } 17 finally 18 { 19 CloseDatabase(); 20 } 21 }
SimpleExamples.vb: GetSortedPilots
01Private Shared Sub GetSortedPilots() 02 Dim container As IObjectContainer = Database() 03 Try 04 Dim result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf AllPilotsMatch, AddressOf PilotPointsCompare) 05 ListResult(result) 06 Catch ex As Exception 07 System.Console.WriteLine("System Exception: " + ex.Message) 08 Finally 09 CloseDatabase() 10 End Try 11 End Sub
SimpleExamples.vb: AllPilotsMatch
1Private Shared Function AllPilotsMatch(ByVal p As Pilot) As Boolean 2 ' each Pilot is included in the result 3 Return True 4 End Function
SimpleExamples.vb: PilotPointsCompare
1Private Shared Function PilotPointsCompare(ByVal p1 As Pilot, ByVal p2 As Pilot) As Integer 2 Return p2.Points - p1.Points 3 End Function

GetPilotsSortByNameAndPoints

Select all pilots, sort descending by name and by points. 

SimpleExamples.cs: GetPilotsSortByNameAndPoints
01public static void GetPilotsSortByNameAndPoints() 02 { 03 IObjectContainer container = Database(); 04 try 05 { 06 IList<Pilot> result = container.Query<Pilot>(delegate(Pilot pilot) 07 { 08 return true; 09 10 }, new System.Comparison<Pilot>( 11 // sort by name then by points: descending 12 delegate(Pilot p1, Pilot p2) 13 { 14 int compareResult = p1.Name.CompareTo(p2.Name); 15 if (compareResult == 0) 16 { 17 return p1.Points - p2.Points; 18 } 19 else 20 { 21 return -compareResult; 22 } 23 24 })); 25 ListResult(result); 26 } 27 finally 28 { 29 CloseDatabase(); 30 } 31 }
SimpleExamples.vb: GetPilotsSortByNameAndPoints
01Public Shared Sub GetPilotsSortByNameAndPoints() 02 Dim container As IObjectContainer = Database() 03 Try 04 Dim result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf AllPilotsMatch, AddressOf PilotPointsAndNameCompare) 05 ListResult(result) 06 Catch ex As Exception 07 System.Console.WriteLine("System Exception: " + ex.Message) 08 Finally 09 CloseDatabase() 10 End Try 11 End Sub
SimpleExamples.vb: AllPilotsMatch
1Private Shared Function AllPilotsMatch(ByVal p As Pilot) As Boolean 2 ' each Pilot is included in the result 3 Return True 4 End Function
SimpleExamples.vb: PilotPointsAndNameCompare
1Private Shared Function PilotPointsAndNameCompare(ByVal p1 As Pilot, ByVal p2 As Pilot) As Integer 2 ' sort by name then by points: descending 3 Dim compareResult As Integer = p1.Name.CompareTo(p2.Name) 4 If (compareResult = 0) Then 5 Return p1.Points - p2.Points 6 Else 7 Return -compareResult 8 End If 9 End Function

GetPilotsSortWithComparator

Sort by points using pre-defined comparator.

SimpleExamples.cs: GetPilotsSortWithComparator
01public static void GetPilotsSortWithComparator() 02 { 03 IObjectContainer container = Database(); 04 try 05 { 06 IList <Pilot> result = container.Query<Pilot>(delegate(Pilot pilot) 07 { 08 return true; 09 10 }, new PilotComparator()); 11 ListResult(result); 12 } 13 finally 14 { 15 CloseDatabase(); 16 } 17 }
SimpleExamples.cs: PilotComparator
01private class PilotComparator : IComparer<Pilot> 02 { 03 public int Compare(Pilot p1, Pilot p2) 04 { 05 int result = p1.Name.CompareTo(p2.Name); 06 if (result == 0) 07 { 08 return p1.Points - p2.Points; 09 } 10 else 11 { 12 return -result; 13 } 14 } 15 }