Sort Array (2D) ? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Sort Array (2D) ?

19 Dec 2018, 11:24

Sorry, I guess, it is already somewhere else, but I can not find it easily.

Code: Select all

myArray := [[2, "Quick"]
, [4, "Brown"]
, [3, "Fox"]
, [1, "Jumps"]]	
For Each, x In myArray
	myResult .= x[1] " " x[2] "`n"
MsgBox % myResult
I'd like make it like this
1 Jumps
2 Quick
3 Fox
4 Brown
Thanks
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Sort Array (2D) ?

19 Dec 2018, 11:31

if u use an object it will sort itself automatically:

Code: Select all

myArray := {2: "Quick"
		  , 4: "Brown"
		  , 3: "Fox"
		  , 1: "Jumps"}	

For i, x In myArray
	myResult .= i " " x "`n"
MsgBox % myResult
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: Sort Array (2D) ?

19 Dec 2018, 11:33

possible workaround is to combine it into 1D, sort and make it 2D again
but... that is not so beautiful...
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: Sort Array (2D) ?

19 Dec 2018, 11:34

swagfag wrote:
19 Dec 2018, 11:31
if u use an object it will sort itself automatically:..
ah.. really ? interesting..
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: Sort Array (2D) ?

19 Dec 2018, 11:37

on second thought..
I could have, for instance, two (2) 2's
Is it OK with the Object ?

[EDIT]
Nah.. it overwrites....
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Sort Array (2D) ?

19 Dec 2018, 11:39

Since objects already sort in the manner that you would like, pass everything to an object and pass them back out into their arrays:

Code: Select all

myArray := [[2, "Quick"]
 ,[4, "Brown"]
 ,[3, "Fox"]
 ,[1, "Jumps"]]

tObj :=	{}	

For each, arr In myArray
	tObj[arr[1]] :=	arr[2]

myArray :=	[]

For n, w in tObj
	myArray.Push([n,w])

For each, arr In myArray
	myResult .= arr[1] " " arr[2] "`n"

MsgBox %	Trim(myResult)
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: Sort Array (2D) ?

19 Dec 2018, 11:43

sinkfaze wrote:
19 Dec 2018, 11:39
Since objects already sort in the manner that you would like, pass everything to an object and pass them back out into their arrays:...
Thanks for the nice tip
But
It overwrites too.

Code: Select all

myArray := [[2, "Quick"]
 ,[4, "Brown"]
 ,[3, "Fox"]
 ,[3, "Over"]
 ,[1, "Jumps"]]
1 Jumps
2 Quick
3 Over
4 Brown
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Sort Array (2D) ?

19 Dec 2018, 12:06

IMEime wrote:
19 Dec 2018, 11:43
It overwrites too.
Pass the elements to a variable, use Sort, parse, split, back to arrays.

Code: Select all

myArray := [[2, "Quick"]
 ,[4, "Brown"]
 ,[3, "Fox"]
 ,[3, "Over"]
 ,[1, "Jumps"]]

tList :=	""

For each, arr In myArray
	tList .=	arr[1] " " arr[2] "`n"

tList :=	RegExReplace(tList,"s)^\v+|\v+$")
Sort, tList

myArray :=	[]

Loop, parse, tList, `n, `r
	_ :=	StrSplit(A_LoopField," "), myArray.Push([_[1],_[2]])

For each, arr In myArray
	myResult .= arr[1] " " arr[2] "`n"

MsgBox %	myResult
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: Sort Array (2D) ?

19 Dec 2018, 15:33

IMEime wrote:
19 Dec 2018, 11:33
possible workaround is to combine it into 1D, sort and make it 2D again
but... that is not so beautiful...
anyway.. thanks "sinkfaze" for your efforts..
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

Re: Sort Array (2D) ?

19 Dec 2018, 15:57

Hi,
It is not obvious to me how one would sort a 2D array. Are you trying to say, sort by rows first, then by columns? or the other way around?
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: Sort Array (2D) ?

19 Dec 2018, 18:53

oif2003 wrote:
19 Dec 2018, 15:57
Hi,
It is not obvious to me how one would sort a 2D array. Are you trying to say, sort by rows first, then by columns? or the other way around?
thanks for comment
you have a point
But
that is minor stuff
that is your choice (or default)
I am not good at C# though
I have tried, have a look at it

Code: Select all

List<List<string>> myArray = 
	new List<List<string>> {				       
		new List<string> { "2", "Quick" },
		new List<string> { "4", "Brown" },
		new List<string> { "3", "Fox" },
		new List<string> { "3", "Over" },
		new List<string> { "1", "Jump" }};
MessageBox.Show(string.Join("\n", 
	myArray
		.OrderBy(x => x[0])
		.ThenBy(x => x[1])
		//.ThenByDescending(x => x[1])
		.Select(x => x[0] + " " + x[1])));

//  1 Jump
//  2 Quick
//  3 Fox
//  3 Over
//  4 Brown

//  1 Jump
//  2 Quick
//  3 Over
//  3 Fox
//  4 Brown
i am not wanted the same procedure like another language, they are there.
i am looking for a way how to sort myArray in AHK
thanks
Last edited by IMEime on 19 Dec 2018, 21:26, edited 2 times in total.
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: Sort Array (2D) ?  Topic is solved

23 Dec 2018, 09:37

Post Closed
"impossible"
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Sort Array (2D) ?

23 Dec 2018, 14:08

I'd like make it like this
Very easy with nnnik's nice sort function

Code: Select all

myArray := [[2, "Quick"]
, [4, "Brown"]
, [3, "Fox"]
, [1, "Jumps"]]	

sort myArray, (a, b) => a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0


For Each, x In myArray
	myResult .= x[1] " " x[2] "`n"
MsgBox  myResult
And it is for v2 :thumbup:

Cheers :xmas:
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: Sort Array (2D) ?

23 Dec 2018, 22:25

thanks
BUT
I am not interested in Version 2

Current line number is 48.000 for my main script only(it is a single file).
-and some lesser lines of scripts are a little bit more than hundred files.

I do not have any kind of Energy to change it into V2
Why do I have to ? Just to sort an Array ?

How about V3 in the near future or V4 ? no, no, no..
User avatar
Chunjee
Posts: 1500
Joined: 18 Apr 2014, 19:05
Contact:

Re: Sort Array (2D) ?

13 Nov 2019, 11:07

IMEime wrote:
19 Dec 2018, 11:24
Sorry, I guess, it is already somewhere else, but I can not find it easily.

I tested this with biga.ahk and it works: https://www.npmjs.com/package/biga.ahk

Code: Select all

A := new biga()
array := [[2, "Quick"]
, [4, "Brown"]
, [3, "Fox"]
, [1, "Jumps"]]

sortedArray := A.sortBy(array, 1)
msgbox, % A.print(sortedArray)
; => [[1, "Jumps"], [2, "Quick"], [3, "Fox"], [4, "Brown"]]
This works because the key of 1 is the order integer you have as the first element in the inner arrays.

I would recommend the following as easier to work with:

Code: Select all

array := [{"order": 2, "value": "Quick"}
, {"order": 4, "value": "Brown"}
, {"order": 3, "value": "Fox"}
, {"order": 1, "value": "Jumps"}]
in which case sorting by the key "order" is clearer like so: A.sortBy(array, "order")
Last edited by Chunjee on 01 Apr 2021, 16:51, edited 1 time in total.
BartmanEH
Posts: 23
Joined: 01 Apr 2021, 15:48

Re: Sort Array (2D) ?

01 Apr 2021, 15:53

Chunjee wrote:
13 Nov 2019, 11:07
I tested this with biga.ahk and it works: https www.npmjs.com /package/biga.ahk Broken Link for safety
How does one install/use biga.ahk given that AHK is for Windowz and Windowz doesn't have npm installer (AFAIK... noob alert)
User avatar
Chunjee
Posts: 1500
Joined: 18 Apr 2014, 19:05
Contact:

Re: Sort Array (2D) ?

01 Apr 2021, 16:55

BartmanEH wrote:
01 Apr 2021, 15:53
How does one install/use biga.ahk given that AHK is for Windowz and Windowz doesn't have npm installer (AFAIK... noob alert)

if you are not familiar with npm or don't have it installed. You may wish to download the library directly from github. All you need is export.ahk which you would #Incude as you would normally a class library.
BartmanEH
Posts: 23
Joined: 01 Apr 2021, 15:48

Re: Sort Array (2D) ?

01 Apr 2021, 18:34

Chunjee wrote:
13 Nov 2019, 11:07
I would recommend the following as easier to work with:

Code: Select all

array := [{"order": 2, "value": "Quick"}
, {"order": 4, "value": "Brown"}
, {"order": 3, "value": "Fox"}
, {"order": 1, "value": "Jumps"}]
in which case sorting by the key "order" is clearer like so: A.sortBy(array, "order")
I am going to try this. Thanks for suggestion on how to use biga.ahk.
BartmanEH
Posts: 23
Joined: 01 Apr 2021, 15:48

Re: Sort Array (2D) ?

06 Apr 2021, 18:28

Chunjee wrote:
01 Apr 2021, 16:55
if you are not familiar with npm or don't have it installed. You may wish to download the library directly from github. All you need is export.ahk Broken Link for safety which you would #Incude as you would normally a class library.
I have manually included export.ahk but the following array is not sorting:

Code: Select all

     Result_Array_Filtered := [{"distance": 10540, "Lat": 40.75423387, "Lon": -73.92452087, "target": "<trkpt lat=""40.75423387"" lon=""-73.92452087""><name>#001</name></trkpt>"}
    ,    {"distance": 47430, "Lat": 40.68004787, "Lon": -73.93168387, "target": "<trkpt lat=""40.68004787"" lon=""-73.93168387""><name>#002</name></trkpt>"}
    ,    {"distance": 1030, "Lat": 40.90804987, "Lon": -73.83052687, "target": "<trkpt lat=""40.90804987"" lon=""-73.83052687""><name>#003</name></trkpt>"}
    ,    {"distance": 3510, "Lat": 40.85642787, "Lon": -73.87724487, "target": "<trkpt lat=""40.85642787"" lon=""-73.87724487""><name>#004</name></trkpt>"}
    ,    {"distance": 144, "Lat": 40.71114687, "Lon": -73.77809887, "target": "<trkpt lat=""40.71114687"" lon=""-73.77809887""><name>#005</name></trkpt>"}]
    
    Result_Array_Filtered_Sorted := A.reverse(A.sortBy(Result_Array_Filtered, "distance"))
The new array is simply reversed but not sorted by Key “distance”—what am I missing?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: DecimalTurn, macromint, peter_ahk and 341 guests