The following examples represent NQ sorting techniques. Store Pilots function is used to fill in the database.
Select all the pilots from the database and sort descending by points.
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
}
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
1Private Shared Function AllPilotsMatch(ByVal p As Pilot) As Boolean 2
' each Pilot is included in the result 3
Return True 4
End Function
1Private Shared Function PilotPointsCompare(ByVal p1 As Pilot, ByVal p2 As Pilot) As Integer 2
Return p2.Points - p1.Points 3
End Function
Select all pilots, sort descending by name and by points.
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
}
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
1Private Shared Function AllPilotsMatch(ByVal p As Pilot) As Boolean 2
' each Pilot is included in the result 3
Return True 4
End Function
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
Sort by points using pre-defined comparator.
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
}
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
}