I am a C student in VBScript coding and need a little help.
My code executes a split command as follows:
outputArray = split(Description," ")
With the individual words from Description now in an array, I want to sort the array based on the string length for each word.
So, for example, if Description is equal to "this is an example of a description" then my array values are [this, is, an, example, of, a, description], right?
But I want to resort the array so that the longest words are first, i.e. the array items are ordered by string length. So, after some VBScript code that I can't seem to figure out, the array would look like this: [description, example, this, an, is, of, a]
If there's a tie for string length, the secondary sort would be alphabetical.
I would greatly appreciate some help on this from an A student out there. Thanks.
here is a helpful link on how to sort by length and in alphabetical order
http://www.webknowhow.net/dir/ASP/FAQ/array_faq.html
As VBScript has no native sort, it needs a little help from a friend. In your case - because of your more complex sorting criteria - the friend should not be .Net's ArrayList, JScript's sort, or sort.exe (introduced here), but a disconnected ADO recordset:
output:
For background start here.
ADDED - For ArrayList Aficionados:
A Disconnected Recordset should be your first choice, if your data is in essence tabular (sort criteria involves more than one aspect/property of the elements).
ArrayList sorting in VBScript is good for simple cases only, because - AFAIK - you can't pass a compare function to the sort method. Please, prove me wrong!
If you must use an ArrayList for more complex sorting, consider the Schwartzian transform:
In code:
output: