Post your working scripts, libraries and tools
User avatar
Posts: 157
Joined: 15 Aug 2014, 06:44
GitHub: bichlepa


19 May 2018, 10:33

This is a powerful and easy to use solution for sorting objects.

ObjectSort() sourcecode:

Code: Select all

/* ObjectSort() by bichlepa
* Description:
*    Reads content of an object and returns a sorted array
* Parameters:
*    obj:              Object which will be sorted
*    keyName:          [optional] 
*                      Omit it if you want to sort a array of strings, numbers etc.
*                      If you have an array of objects, specify here the key by which contents the object will be sorted.
*    callBackFunction: [optional] Use it if you want to have custom sort rules.
*                      The function will be called once for each value. It must return a number or string.
*    reverse:          [optional] Pass true if the result array should be reversed
objectSort(obj, keyName="", callbackFunc="", reverse=false)
	temp := Object()
	sorted := Object() ;Return value
	for oneKey, oneValue in obj
		;Get the value by which it will be sorted
		if keyname
			value := oneValue[keyName]
			value := oneValue
		;If there is a callback function, call it. The value is the key of the temporary list.
		if (callbackFunc)
			tempKey := %callbackFunc%(value)
			tempKey := value
		;Insert the value in the temporary object.
		;It may happen that some values are equal therefore we put the values in an array.
		if not isObject(temp[tempKey])
			temp[tempKey] := []
	;Now loop throuth the temporary list. AutoHotkey sorts them for us.
	for oneTempKey, oneValueList in temp
		for oneValueIndex, oneValue in oneValueList
			;And add the values to the result list
			if (reverse)
	return sorted
Demonstration script:
You will need string-object-file.ahk. You can get a copy here.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.

#include Object sort.ahk
#include string-object-file.ahk

MyArray:=["Dog", "Cat", "Mouse", "Snake", "Bird"]

MsgBox % "List of animals: `n`n" strobj(MyArray) "`n`nAlphabetically sorted:`n`n" strobj(MySortedArray)

;Source of car list: https://www.globalcarsbrands.com/top-10-fastest-cars-in-the-world/
MyCarList.push({name: "ZENVO ST 1", speedMPH: "233"})
MyCarList.push({name: "HENNESSEY VENOM GT", speedMPH: "270"})
MyCarList.push({name: "KOENIGSEGG AGERA R", speedMPH: "273"})
MyCarList.push({name: "ASTON MARTIN ONE-77", speedMPH: "220"})
MyCarList.push({name: "PAGANI HUAYRA", speedMPH: "230"})
MyCarList.push({name: "MCLAREN F1", speedMPH: "241"})
MyCarList.push({name: "BUGATTI VEYRON SUPER SPORT", speedMPH: "268"})
MyCarList.push({name: "KOENIGSEGG CCR", speedMPH: "242"})
MyCarList.push({name: "SSC ULTIMATE AERO", speedMPH: "256"})
MyCarList.push({name: "9FF GT9-R", speedMPH: "257"})

MySortedCarList:=ObjectSort(MyCarList, "speedMPH",,true)

MsgBox % "Fastest cars: `n`n" strobj(MyCarList) "`n`nSorted by speed:`n`n" strobj(MySortedCarList)

myImmovables.push({town: "New York", size: "60", price: 400000, balcony: 1})
myImmovables.push({town: "Berlin", size: "45", price: 230000, balcony: 1})
myImmovables.push({town: "Moscow", size: "80", price: 350000, balcony: 0})
myImmovables.push({town: "Tokyo", size: "90", price: 600000, balcony: 2})
myImmovables.push({town: "Palma de Mallorca", size: "250", price: 1100000, balcony: 3})

MySortedImmovablesBySize:=ObjectSort(myImmovables, "size")
MySortedImmovablesByBalconyCount:=ObjectSort(myImmovables, "balcony",,true)
MySortedImmovablesPricePerSqM:=ObjectSort(myImmovables, ,"calcPricePerSqM")

	return Immovable.price / Immovable.size

MsgBox % "My immovables: `n`n" strobj(myImmovables)
MsgBox % "My immovables sorted by size: `n`n" strobj(MySortedImmovablesBySize)
MsgBox % "My immovables sorted by balcony count: `n`n" strobj(MySortedImmovablesByBalconyCount)
MsgBox % "My immovables sorted by price per square meter: `n`n" strobj(MySortedImmovablesPricePerSqM)
Output of the demonstration script:
Scripting is too complicated? Try AutoHotFlow, the graphical automation tool! Written in AutoHotkey.
Posts: 971
Joined: 22 Jan 2017, 19:37

Re: ObjectSort()

20 May 2018, 09:02

Thank you, bichlepa. Great documentation in the comments, and I'm sure to make use of this.
User avatar
Posts: 126
Joined: 12 Jan 2016, 15:46

Re: ObjectSort()

28 May 2018, 20:03

This looks great. And I'm glad to see very clear examples: a lack of examples often makes it take an hour before I get a script to work. I'm bookmarking this, for I will certainly want to use it in the future.
User avatar
Posts: 163
Joined: 23 Oct 2013, 05:08

Re: ObjectSort()

29 May 2018, 08:02

Thanks for sharing!

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 33 guests