ObjectSort()
Posted: 19 May 2018, 10:33
This is a powerful and easy to use solution for sorting objects.
ObjectSort() sourcecode:
Demonstration script:
You will need string-object-file.ahk. You can get a copy here.
Output of the demonstration script:
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]
else
value := oneValue
;If there is a callback function, call it. The value is the key of the temporary list.
if (callbackFunc)
tempKey := %callbackFunc%(value)
else
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] := []
temp[tempKey].push(oneValue)
}
;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)
sorted.insertAt(1,oneValue)
else
sorted.push(oneValue)
}
}
return sorted
}
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"]
MySortedArray:=ObjectSort(MyArray)
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:={}
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:={}
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")
calcPricePerSqM(Immovable)
{
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)