-
Notifications
You must be signed in to change notification settings - Fork 5
kiwi.util.Comparator
Nikos Siatras edited this page Sep 29, 2022
·
3 revisions
Comparator is a comparison function, which imposes a total ordering onsome collection of objects. Comparators can be passed to a sort method such as Collections.sort, Arrays.sort(Object[],Comparator) etc.
If you are looking for example code on how to sort an ArrayList using a comparator then visit the following link
https://github.com/nsiatras/kiwi/wiki/kiwi.util.ArrayList#example---how-to-sort-an-arraylist-holding-standard-type-values
The following example will sort an Array holding 10 random double values.
#include once "kiwi\kiwi.bi"
' Initialize a new Array for Double Values
Dim values(0 to 9) as Double
' Add 10 Random double values to myArrayList
for i as Integer = 0 to 9
values(i) = Math.random()
next i
print "Array Elements Before Sort:"
for i as Integer = 0 to ubound(values)
print "Element " & i &" = " & values(i)
next
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Define a comparator Type
MACRO_DefineComparator(Double)
Type myComparator extends Comparator(Double)
declare function compare(a as Double, b as Double) as Integer
End Type
function myComparator.compare(a as Double, b as Double) as Integer
return iif(a>=b, 1, -1) ' Ascending
'return iif(a<=b , 1, -1) 'Descending
end function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Initialize a comparator and use it to sort the array
Dim c as myComparator
c.quickSort(values(),0, ubound(values))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
print ""
print "Array Elements After Sort:"
for i as Integer = 0 to ubound(values)
print "Element " & i &" = " & values(i)
next- MySQL/MariaDB - Coming to v1.0.2
- ArrayList
- Comparator
- HashMap - Coming to v1.0.2
- Queue