seandisanti Posted December 19, 2005 Share Posted December 19, 2005 (edited) i made this for a friend, he suggested posting it here for others.... here ya go. expandcollapse popup#include<array.au3> #cs Title: _ArraySortByLength Version: v2.0 FileName: ArraySortByLength.au3 Description: Takes a single dimenstion array (with 0 being cound of elements, and returns the array sorted in ascending _ or descending order by length of each element Author(s) CameronsDad - Sean - AIM: SiCKPrLck #ce Func _ArraySortByLength($InArray,$ascending = 1) Dim $TempArray[$InArray[0]+1][2] $TempArray[0][0] = $InArray[0] For $x = 1 to $InArray[0] $TempArray[$x][0] = $InArray[$x] $TempArray[$x][1] = Number(StringLen($InArray[$x])) Next Dim $inner,$outer,$temp,$temp2 $n = $temparray[0][0] $h = 1 While $h <= $n/3 $h = $h * 3 + 1 WEnd While $h >0 For $outer = $h To $n $temp = $temparray[$outer][0] $temp2 = Number(StringStripWS($temparray[$outer][1],1)) $inner = $outer While $inner > ($h - 1) And Number(StringStripWS($temparray[$inner-$h][1],1)) >= Number($temp2) $temparray[$inner][0] = $temparray[$inner-$h][0] $temparray[$inner][1] = $temparray[$inner-$h][1] $inner = $inner - $h WEnd $temparray[$inner][0] = $temp $temparray[$inner][1] = $temp2 Next $h = ($h - 1)/3 WEnd For $y = 1 to $TempArray[0][0] $InArray[$y] = $TempArray[$y][0] Next If Not $ascending Then _ArrayReverse($InArray,1) Return($InArray) EndFunc ***edit*** updated to 2.0, changed the sort algo for a faster one, this one sorts the same data in about 10% of the time required of the original. The old one was still pretty fast for most arrays, but with hundreds of elements, it started slowing down. Using an array of 1000 elements, this sort was on average 10 times faster. Edited December 21, 2005 by cameronsdad Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted December 19, 2005 Moderators Share Posted December 19, 2005 i made this for a friend, he suggested posting it here for others.... here ya go. And you gave it the the option for decending or not too! Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
seandisanti Posted December 19, 2005 Author Share Posted December 19, 2005 And you gave it the the option for decending or not too! i was going to make it for multi-dimension arrays also, but then i started thinking of how many times i've ever had to sort an array, single or multi dimentional, by element length, and this morning was the first time EVER... If there's a big demand for this to be multi-dimensional i'll add it in, but i think you're the only one that'll ever use it Link to comment Share on other sites More sharing options...
Uten Posted October 31, 2006 Share Posted October 31, 2006 Hi camronsdad, I know this is an old thread but I was wondering if you had any samples for this code. My test did not turn out well so I probably use it for something it is not supposed to or? Func test_ArraySortByLength() Local $a = _ArrayCreate("aa","aab","abc","ab","acde","abc") Local $a2 $a2 = _ArraySortByLength($a) Local $i For $i = 0 to UBound($a2) -1 ConsoleWrite($a2[$i] & @LF) Next ConsoleWrite( @LF) $a2 = _ArraySortByLength($a, 0) For $i = 0 to UBound($a2) -1 ConsoleWrite($a2[$i] & @LF) Next EndFunc Gives: aa aab abc ab acde abc aa abc acde ab abc aab Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now