Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

# logical Human Sorting Function

3 replies to this topic
• Members
• 24 posts
• Last active: Jul 11 2016 11:17 PM
• Joined: 30 Mar 2015

Hi, I was looking for a sort function that sorts like humans do.

As most sort sort like: 1,11,12,13,14,2,21,23,26,28,3,31,32,35,38,37, Etc

I wanted to have: 1,2,5,7,9,10,155,266,467, Etc

So I made a function that would do just that.

It isn't pretty as im not realy proficiant, but it does the job.

It only works with integers (? Dont know if correct name) Such as -1000-till Infinity

01,02 wont work.

Works realy simple:

Creates an psuado array from CSV

Adds +1 and checks any of the Array components if its in there.

If it is, it adds it to the list, else adds +1 and checks again.

Could be done in reverse with some modification.

Feel free to use, edit, or do whatever you like with it.

This Probably already exists( InsertionSort()) but the Noob(me) can now have a better grasp on how it works xD

```#SingleInstance Force
SetBatchLine -1

^K::
MyVar = 60,444,1,5,77,1174,55,83,936,6283,9,10,-1
Msgbox % Sorting(MyVar)
return

Sorting(Var)
{
StringLen,Length,Var
Loop, Parse, Var, `,
{
Count := A_Index ; Adds new item to the list
Item%Count% := A_LoopField
TotalItems := A_Index ; Number of Section Created
Tooltip % item%Count% "  " TotalItems
; Sleep, 500
index = -10000 ; From What Minus Value you can Re-Arange
}
While (LengthSorted != Length) ;if length not Equal
{
index ++ ; Adds +1
Loop % TotalItems ;Loops total Va
{
CurrentItem := Item%A_Index%
if Index in %CurrentItem%  ;if Index(2,3,4,5,6,Etc) Equals Item%A_Index%
{
SortStr .= item%A_Index% "," ;Add to SortStr
StringLen,SortedLength,SortStr
lengthSorted := SortedLength-1 ;Removes last , For counting purpose
Tooltip % SortStr
}
}
}
StringTrimRight,SortStr,SortStr,1 ; removes last Comma ","
Return SortStr
}
```

• Members
• 868 posts
• Last active: Jan 25 2016 01:29 AM
• Joined: 04 Mar 2013

Hi Wyvern010,

Now that you understand one way to do it, you can use the built-in way!

```#IfWinActive
!z::
string := "1,11,12,13,14,2,21,23,26,28,3,31,32,35,38,37"
Sort, string, N D,
msgbox % string
return
```

See: Sort

My script runs in A_AhkVersion:=1.1.22.07, get the latest version at http://ahkscript.org/download/

Check out this AutoHotkey tutorial: http://ahkscript.git...o/AHK_Tutorial/

Read the documentation: http://ahkscript.org.../AutoHotkey.htm

• Members
• 24 posts
• Last active: Jul 11 2016 11:17 PM
• Joined: 30 Mar 2015
I tried that!
But i didn't get the desired results.
Maybe because my var was unquoted?
The program that is used it for reads values from game memory, and no matter what option i used, it wasn't nicely sorted.

• Members
• 868 posts
• Last active: Jan 25 2016 01:29 AM
• Joined: 04 Mar 2013

I can't say why you had trouble but the input has to be a string so both below work:

```string := "1,11,12,13,14,2,21,23,26,28,3,31,32,35,38,37"
string = 1,11,12,13,14,2,21,23,26,28,3,31,32,35,38,37
```

but not this:

```string := 1,11,12,13,14,2,21,23,26,28,3,31,32,35,38,37
```

My script runs in A_AhkVersion:=1.1.22.07, get the latest version at http://ahkscript.org/download/

Check out this AutoHotkey tutorial: http://ahkscript.git...o/AHK_Tutorial/

Read the documentation: http://ahkscript.org.../AutoHotkey.htm