Questin about arrays

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

Questin about arrays

06 Apr 2018, 02:31

Plz help. I have array with coordinates.

Code: Select all

Array := []
Array[1,1] := 4844.2578125000 ;x
Array[1,2] := 4740.5839843750 ;y

Array[2,1] := 4905.8173828125 ;x
Array[2,2] := 4721.2353515625 ;y

Array[3,1] := 4860.1962890625 ;x
Array[3,2] := 4724.5092773438 ;y

Array[4,1] := 4893.0820312500 ;x
Array[4,2] := 4726.9536132813 ;y

Array[5,1] := 4876.6328125000 ;x
Array[5,2] := 4726.5429687500 ;y

MsgBox % Array.Length()
Question: How to sort it (ASC)?
Last edited by masheen on 06 Apr 2018, 13:48, edited 1 time in total.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Questin about arrays

06 Apr 2018, 10:51

masheen wrote:Plz help. I have array with coordinates.

Code: Select all

Array := []
Array[1,1] := 4844.2578125000 ;x
Array[1,2] := 4740.5839843750 ;y

Array[2,1] := 4860.1962890625 ;x
Array[2,2] := 4724.5092773438 ;y

Array[3,1] := 4876.6328125000 ;x
Array[3,2] := 4726.5429687500 ;y

Array[4,1] := 4893.0820312500 ;x
Array[4,2] := 4726.9536132813 ;y

Array[5,1] := 4905.8173828125 ;x
Array[5,2] := 4721.2353515625 ;y
MsgBox % Array.Length()
Question: How to sort it (ASC)?
I am not totally sure how you want them sorted but if you are talking about sorting them by the x or y value, here is an example.

Code: Select all

Array := []
Array[1,1] := 4844.2578125000 ;x
Array[1,2] := 4740.5839843750 ;y

Array[2,1] := 4860.1962890625 ;x
Array[2,2] := 4724.5092773438 ;y

Array[3,1] := 4876.6328125000 ;x
Array[3,2] := 4726.5429687500 ;y

Array[4,1] := 4893.0820312500 ;x
Array[4,2] := 4726.9536132813 ;y

Array[5,1] := 4905.8173828125 ;x
Array[5,2] := 4721.2353515625 ;y

SortedBy1 := Sort_Array(Array,1) ;x
SortedBy2 := Sort_Array(Array,2) ;y

Sort_Array(Arr, By) 
{
	Temp := {}
	for k, v in Arr
		Temp[v[By]] := v
	Arr := {}
	for k, v in Temp
		Arr.Push(v)
	return Arr
}
This is sorting them as strings and not numbers so it is just a start. Stuff like 98.76 vs 123.4 might give unexpected results. You could pad the left with zeros to make all the numbers the same length for better number sort results.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 281 guests