Sorting arrays


Continuing the discussion from Storing empty array in Attribute Dictionaries:

The Ruby core Array#sort method ? It also takes a block where you can code a custom sorting algorithm.

Yea, that’s a bit vague. What are the keys ? If the numeric values are the hash values, then are the keys going to just be integers? If so, there is no need for a separate hash since you sorted the array and access by integer index is built-in.


Ok I start with an un-ordered array with a bunch of window names: [“window1”, “window2”, “window7”, “window3”]

The names of the windows does not matter in this array other than for each listed window in the list there exists a library attribute that contains its (the windows) location from the origin of the wall panel, some float, lets say 74.5323 inches.

Since the windows can be inserted into the wall in any location or order the location of each window is in no way indicated by the name of that window.

What I would like is an ordered array/list the windows locations ie. [42.253, 52.25, 70.2343, 112.5] and a corresponding array/list with window names listed and sorted to match the same order as the location list.

Can you sort an array of arrays?


Yes but you’d likely need to use the custom block form.


This seems to work:

if @Openinglist.length > 1
		@Openinglist_order = @Openinglist.sort do |a, b|
		a[1] <=> b[1]

		puts @Openinglist_order


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.