Sort Array Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
inseption86
Posts: 206
Joined: 19 Apr 2018, 00:24

Sort Array

28 Feb 2024, 06:16

Good afternoon Tell me how to sort array2 according to array1?

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance force   ; игнор уже запущенного скрипта, переоткрытие
Setbatchlines -1 


list =
(
21864483
21864495
21962071
21974825
22049592
22041919
22018747
22041979
22009156
22052891
22052700
22052687
22060486
22037357
22044142
)

array := {}
loop, parse, % list, `n `r
	array.Push(A_LoopField)


list2 =
(
21864483	X
21864495	2
21962071	1
21974825	1
22009156	1
22018747	X
22037357	2
22041919	X
22041979	X
22044142	2
22049592	1
22052687	12
22052700	X2
22052891	2
22060486	1
)

array2 := {}
loop, parse, % list2, `n `r
	array2.Push(A_LoopField)


ExitApp
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Sort Array  Topic is solved

28 Feb 2024, 06:43

Code: Select all

; This script sorts an array according to a list of values
#Requires AutoHotkey v1.1.33.11
Gosub Init   ; Create arrays
sorted := [] ; New sorted array

; Sort array
Next:
For n1, item1 in array1
 For n2, item2 in array2
  If InStr(item2, item1 "`t") = 1 {
   sorted.Push(item2)
   Continue Next
  }

; Display results
str := ""
For each, item in sorted
 str .=  item "`n"
MsgBox 262208, Result, % Trim(str, "`n")

Return

Init:
list1 := "
(
21864483
21864495
21962071
21974825
22049592
22041919
22018747
22041979
22009156
22052891
22052700
22052687
22060486
22037357
22044142
)"
list2 := "
(
21864483	X
21864495	2
21962071	1
21974825	1
22009156	1
22018747	X
22037357	2
22041919	X
22041979	X
22044142	2
22049592	1
22052687	12
22052700	X2
22052891	2
22060486	1
)"
array1 := [], array2 := []
Loop Parse, list1, `n, `r ; Create the first array
 array1.Push(A_LoopField)
Loop Parse, list2, `n, `r ; Create the second array
 array2.Push(A_LoopField)
Return
If you already have plain-text listings, then arrays are not needed to sort the values.

This makes no adjustments for unmatched items or duplicates in the index.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], peter_ahk and 341 guests