Sort contents of objects in original input order

Get help with using AutoHotkey and its commands and hotkeys
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Sort contents of objects in original input order

10 Jul 2015, 05:49

Code: Select all

Apps:={}
Apps["Manus"] := {Name: "MMConsoleAdmin", Checkbox: 0, Process: "MMConsoleAdmin_2007.exe", Registry: "Server"}
Apps["Media"] := {Name: "MMMediaAdministrator", Checkbox: 0, Process: "MMMediaAdministrator.exe", Registry: "Server"}
Apps["AVAutomation"] := {Name: "MMAVAutomation", Checkbox: 0, Process: "MMAVAutomation.exe", Registry: "Server"}
 
for a in Apps
{
	msgbox % Apps[a].Name
}
How can I make it return MMConsoleAdmin first?
gu3st

Re: Sort contents of objects in original input order

10 Jul 2015, 06:06

As far as I know ahk doesn't set a specific order so you can never know unless you set it yourself, something like this should do the trick - you can now use .InsertAt() and RemoveAt() to alter the order

Code: Select all

Apps:={}
Apps[1] := {Type:"Manus", Name: "MMConsoleAdmin", Checkbox: 0, Process: "MMConsoleAdmin_2007.exe", Registry: "Server"}
Apps[2] := {Type:"Media", Name: "MMMediaAdministrator", Checkbox: 0, Process: "MMMediaAdministrator.exe", Registry: "Server"}
Apps[3] := {Type:"AVAutomation", Name: "MMAVAutomation", Checkbox: 0, Process: "MMAVAutomation.exe", Registry: "Server"}
 
for a in Apps
	{
	 msgbox % Apps[a].Name
	}
Neuromancer
Posts: 10
Joined: 24 May 2015, 05:02

Re: Sort contents of objects in original input order

10 Jul 2015, 06:32

With help from GeekDude's custom enum thread i made this, it's first time i get to make anything with custom objects methods etc so it may be a better way!

Code: Select all

class test
{
	order := []
	
	__Set(k){
		this.order.push(k)
	}
	
	_NewEnum(){
		return new this.customEnum(this)
	}
	
	class customEnum
	{
		__New(obj){
			this.obj := obj
			this.order := obj.order
		}
		
		Next(ByRef k, ByRef v := ""){
			if (this.order[1]){
				k := this.order.RemoveAt(1)
				v := this.obj[k]
				return true
			}
			return false		
		}
	}
}

obj := new test()

obj.c := 1
obj.b := 2
obj.a := 3

for k, v in obj
	msgBox % k " = " v

edit: new code
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Re: Sort contents of objects in original input order

13 Jul 2015, 15:46

Neuromancer: Thanks, although. I'm having a problem with this, it won't return the second msgbox (first for loop empties it). Any idea why?

https://bpaste.net/show/0fd0b95149b7
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Re: Sort contents of objects in original input order

14 Jul 2015, 01:24

You can even run your own example with a second for loop.
Neuromancer
Posts: 10
Joined: 24 May 2015, 05:02

Re: Sort contents of objects in original input order

14 Jul 2015, 05:55

Code: Select all


class test
{
	order := []
 
	__Set(k){
		this.order.push(k)
	}
 
	_NewEnum(){
		return new this.customEnum(this)
	}
 
	class customEnum
	{
		__New(obj){
			this.obj := obj
			this.order := obj.order
			this.counter := 1
		}
 
		Next(ByRef k, ByRef v := ""){
			if (this.counter <= this.order.maxIndex()){
				k := this.order[this.counter++]
				v := this.obj[k]
				return true
			}
			return false		
		}
	}
}

i will have to review that code, that's a temp fix, this code sucks hard ^^

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Bing [Bot], mikeyww, Peaceful and 50 guests