New answers tagged

4

Quoting Joe Armstrong (emphasis mine): The original reason was that there should be a defined total order over all terms (why? - so that we could write generic sorting algorithms that could order any terms). The actual order was based on the idea of "complexity" an integer is "simpler" than an atom. a tuple is simpler than a list and so on.. ...


-1

# ruby syntax def compare_them (obj1, obj2) if obj1[0].nil? if !obj2[0].nil? return obj1[1] <=> obj2[0] else if obj2[1].nil? if obj1[1] == obj2[0] return -1 else return obj1[1] <=> obj2[0] end else return obj1[1] <=> obj2[1] end end else if ...


1

Most languages allow a custom comparator function to be supplied to the sort. See Java Comparator One use of a custom comparator, for example, is to sort based on two attributes (a primary an secondary) with one sort. A comparator for this will first compare the primary attributes, then only if they are equal, will check the secondary attribute. A custom ...


2

To guarantee ascending order in both columns we can compare by column when there is no nil and by max value when there is: .sort { |(a0, a1), (b0, b1)| if [a0, b0].none?(&:nil?) a0 <=> b0 elsif [a1, b1].none?(&:nil?) a1 <=> b1 else a0, a1, b0, b1 = [a0, a1, b0, b1].map(&:to_i) [[a0, a1].max, a0, a1] <=> ...


-1

Ah...I think I may have an answer. Here's a two-pass approach (calling each pair [x, y]). Select all pairs where x is not nil. Sort them by x. Call the resulting list s. Select all pairs where x is nil. Insert them into s at the appropriate places (I'm not sure how you determine the appropriate places, though). Does that help?



Top 50 recent answers are included