Compare a and b.

Returns:

  • -1 if a < b
  • 0 if a = b
  • 1 if a > b

Property definitions

core $ Comparator :: compare
	# Compare `a` and `b`.
	#
	# Returns:
	#
	# * -1 if a < b
	# * 0  if a = b
	# * 1  if a > b
	fun compare(a: COMPARED, b: COMPARED): Int is abstract
lib/core/collection/sorter.nit:27,2--34,55

core $ MapComparator :: compare
	redef fun compare(a,b) do return comparator.compare(map[a], map[b])
lib/core/collection/sorter.nit:334,2--68

core $ DefaultComparator :: compare
	# Return a <=> b
	redef fun compare(a, b) do return a <=> b
lib/core/collection/sorter.nit:342,2--343,42

core $ DefaultReverseComparator :: compare
	# Returns `b <=> a`.
	redef fun compare(a, b) do return b <=> a
lib/core/collection/sorter.nit:355,2--356,42

core $ CachedAlphaComparator :: compare
	redef fun compare(a, b) do
		return do_to_s(a) <=> do_to_s(b)
	end
lib/core/text/abstract_text.nit:2491,2--2493,4

core $ AlphaComparator :: compare
	redef fun compare(a, b) do
		if a == b then return 0
		if a == null then return -1
		if b == null then return 1
		return a.to_s <=> b.to_s
	end
lib/core/text/abstract_text.nit:2499,2--2504,4

ai $ NodeComparator :: compare
	redef fun compare(a,b) do return a.score <=> b.score
lib/ai/search.nit:611,2--53

counter $ CounterComparator :: compare
	redef fun compare(a,b) do return self.counter.map[a] <=> self.counter.map[b]
lib/counter/counter.nit:345,2--77

event_queue $ EventComparator :: compare
	redef fun compare(a, b) do return a.start <=> b.start
lib/event_queue/event_queue.nit:382,2--54

functional $ ComparatorWith :: compare
        redef fun compare(a,b)
        do
                var x = f.call(a)
                var y = f.call(b)
                if x < y then return -1
                if x > y then return 1
                return 0
        end
lib/functional/iter_extras.nit:400,9--407,11

gamnit $ DrawOrderComparator :: compare
	# Require: `a isa SpriteContext and b isa SpriteContext`
	redef fun compare(a, b)
	do return a.as(SpriteContext).draw_order <=> b.as(SpriteContext).draw_order
lib/gamnit/flat/flat_core.nit:1763,2--1765,76

geometry $ PointXCompare :: compare
	redef fun compare(a,b) do
		if a.x == b.x then
			if a.y == b.y then return 0
			if a.y > b.y then return - 1
			return 1
		else
			if a.x > b.x then return -1
			return 1
		end
	end
lib/geometry/polygon.nit:296,2--305,4

geometry $ CounterClockWiseSort :: compare
	redef fun compare(a,b) do
		if a.x == b.x and a.y == b.y then return 0
		if a.x - center.x >= 0.0 and b.x - center.x < 0.0 then return -1
		if a.x - center.x < 0.0 and b.x - center.x >= 0.0 then return 1
		if a.x - center.x == 0.0 and b.x - center.x == 0.0 then
			if a.y - center.y >= 0.0 or b.y - center.y >= 0.0 then
				if a.y > b.y then return -1
				return 1
			end
			if b.y > a.y then return -1
			return 1
		end

		var det = (a.x - center.x) * (b.y - center.y) - (b.x - center.x) * (a.y - center.y)
		if det > 0.0 then return 1
		if det < 0.0 then return -1

		var d1 = (a.x - center.x) * (a.x - center.x) + (a.y - center.y) * (a.y - center.y)
		var d2 = (b.x - center.x) * (b.x - center.x) + (b.y - center.y) * (b.y - center.y)
		if d1 > d2 then return -1
		return 1
	end
lib/geometry/polygon.nit:340,2--361,4

geometry $ ClockWiseSort :: compare
	redef fun compare(a,b) do
		if a.x == b.x and a.y == b.y then return 0
		if a.x - center.x >= 0.0 and b.x - center.x < 0.0 then return 1
		if a.x - center.x < 0.0 and b.x - center.x >= 0.0 then return -1
		if a.x - center.x == 0.0 and b.x - center.x == 0 then
			if a.y - center.y >= 0.0 or b.y - center.y >= 0.0 then
				if a.y > b.y then return 1
				return -1
			end
			if b.y > a.y then return 1
			return -1
		end

		var det = (a.x - center.x) * (b.y - center.y) - (b.x - center.x) * (a.y - center.y)
		if det > 0.0 then return -1
		if det < 0.0 then return 1

		var d1 = (a.x - center.x) * (a.x - center.x) + (a.y - center.y) * (a.y - center.y)
		var d2 = (b.x - center.x) * (b.x - center.x) + (b.y - center.y) * (b.y - center.y)
		if d1 > d2 then return 1
		return -1
	end
lib/geometry/polygon.nit:368,2--389,4

vsm $ IndexMatchSorter :: compare
	redef fun compare(a, b) do
		return b.similarity <=> a.similarity
	end
lib/vsm/vsm.nit:397,2--399,4

poset $ POSet :: compare
	# Compare two elements in an arbitrary total order.
	#
	# This function is mainly used to sort elements of the set in an coherent way.
	#
	# ~~~~
	# var pos = new POSet[String]
	# pos.add_chain(["A", "B", "C", "D", "E"])
	# pos.add_chain(["A", "X", "C", "Y", "E"])
	# var a = ["X", "C", "E", "A", "D"]
	# pos.sort(a)
	# assert a == ["E", "D", "C", "X", "A"]
	# ~~~~
	#
	# POSet are not necessarily total orders because some distinct elements may be incomparable (neither greater or smaller).
	# Therefore this method relies on arbitrary linear extension.
	# This linear extension is a lawful total order (transitive, anti-symmetric, reflexive, and total), so can be used to compare the elements.
	#
	# The abstract behavior of the method is thus the following:
	#
	# ~~~~nitish
	# if a == b then return 0
	# if has_edge(b, a) then return -1
	# if has_edge(a, b) then return 1
	# return -1 or 1 # according to the linear extension.
	# ~~~~
	#
	# Note that the linear extension is stable, unless a new node or a new edge is added.
	redef fun compare(a, b)
	do
		var ae = self.elements[a]
		var be = self.elements[b]
		var res = ae.tos.length <=> be.tos.length
		if res != 0 then return res
		return elements[a].count <=> elements[b].count
	end
lib/poset/poset.nit:288,2--322,4