Basic programming language :: thinBasic Community
September 10, 2010, 09:36:52 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: RobotDuel - Game for 2 players: http://community.thinbasic.com/index.php?topic=305.0
 
  Home Help thinGaming Search Calendar Gallery Articles Donations Tags Media Center Login Register   *

thinBasic 1.8.0.0 is out !  :: See this post ( last update: 2010.03.07 ! )  :: Latest beta version: thinBasic beta 1.8.6.0 ( last update: 2010.08.02 ! )
Main menu
Members
Total Members: 526
Latest: jnash67
Stats
Total Posts: 26734
Total Topics: 3548
Online Today: 31
Online Ever: 121
(August 16, 2009, 07:08:32 am)
Users Online
Users: 1
Guests: 6
Total: 7
2 Spiders, 6 Guests, 1 User
Eros Olmi
Yahoo spider, Google spider
Subject Statistics
Topic: ARRAY SORT Replies: 16 posts
Read 839 times 0 Members and 1 Guest are viewing this topic.
Pages: 1 2
  Add bookmark  |  Print  
Author Topic: ARRAY SORT  (Read 839 times)
0 Members and 1 Guest are viewing this topic.
Michael Clease
Sr. Member
****

Karma: 24
Offline

Gender: Male
Posts: 1226


Format c:\

OS:
Windows XP Windows XP
Browser:
Firefox 3.5.7 Firefox 3.5.7


« on: February 07, 2010, 12:38:38 am »

I've suspect this for a while and thought I better prove it, the array sort doesnt sort correctly it doesnt take into account string length.

In my example the input should match the output but because string length is not checked it ignores the fact Word10 has an extra character and inserts it between Word1 and Word2 but it should appear at the end of the list.

Let me know what you think.

Mike
Attached files
* Array Sort test.tbasicc (0.59 KB - downloaded 11 times.)
Logged

Why is the world full of nobs trying to push my buttons!!

Home Desktop : WinXP Pro SP3 - Intel Pentium (D)   - 3.0 Ghz - 2GB - Geforce 6800GS
Home Laptop   : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
Home Laptop   : WinXP Pro SP3 - Intel Pentium 4 - 3 Ghz - 512 MB - ATI Mobile Radeon 7000 IGP
Work Desktop  : WinXP Pro SP3 - Intel core 2 Quad Q8200 - 2.33 Ghz - 2 Gb - Quadro Fx 370
Michael Hartlef
Creator of TBDI, TBAI and TBEM
Global Moderator
Hero Member
*****

Karma: 42
Offline

Gender: Male
Posts: 2961


OS:
Mac OS X 10.5 Mac OS X 10.5
Browser:
Firefox 3.5.7 Firefox 3.5.7


« Reply #1 on: February 07, 2010, 10:49:25 am »

Hi Michael,

as I agree with you that word10 should come after word2, its a normal behaviour when sorting strings. They get sorted after the ASC value of each character. If you have access to EXCEL, it does it the same.

Michael

Logged
Eros Olmi
Administrator
Hero Member
*****

Karma: 92
Online

Gender: Male
Posts: 6319


thinBasic co-author

OS:
Windows XP Windows XP
Browser:
Microsoft Internet Explorer 7.0 Microsoft Internet Explorer 7.0


WWW
« Reply #2 on: February 07, 2010, 11:24:47 am »

Mike,

to let others understanding here the data (original and after sorting) that Mike is using:
OriginalAfter sorting
Word1Word1
Word2Word10
Word3Word2
Word4Word3
Word5Word4
Word6Word5
Word7Word6
Word8Word7
Word9Word8
Word10Word9

ARRAY SORT uses ASCII sorting for strings and it compares equivalent ASCII code in the corresponding byte position
So "Word10" comes just after "Word1" and just before "Word2" because the 5th char is "1"

As Michael stated:
  • Excel (any version up to 2007) does the same
  • MSAccess (up to version 2007) does the same

But I do not want justify that current behave MUST be the only way.
For sure is not a bug so I will move this post in "Feature request" forum.
Instead it is and interesting observation that can bring to a new feature.
Considering how current ARRAY SORT works internally, honestly at the moment I do not know how to develop this new behave but worth to think about it.

Ciao
Eros
Logged

www.thinbasic.com | community.thinbasic.compsch.thinbasic.com
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
Michael Clease
Sr. Member
****

Karma: 24
Offline

Gender: Male
Posts: 1226


Format c:\

OS:
Windows XP Windows XP
Browser:
Firefox 3.6 Firefox 3.6


« Reply #3 on: February 07, 2010, 07:07:41 pm »

But what kind of sorting is windows using for file details view its not what you are describing.

If I had the files

Fred-10.txt
Fred-11.txt
Fred-100.txt

the above is how they would be sorted and I would say that is a true sorting.

Logged

Why is the world full of nobs trying to push my buttons!!

Home Desktop : WinXP Pro SP3 - Intel Pentium (D)   - 3.0 Ghz - 2GB - Geforce 6800GS
Home Laptop   : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
Home Laptop   : WinXP Pro SP3 - Intel Pentium 4 - 3 Ghz - 512 MB - ATI Mobile Radeon 7000 IGP
Work Desktop  : WinXP Pro SP3 - Intel core 2 Quad Q8200 - 2.33 Ghz - 2 Gb - Quadro Fx 370
Eros Olmi
Administrator
Hero Member
*****

Karma: 92
Online

Gender: Male
Posts: 6319


thinBasic co-author

OS:
Windows XP Windows XP
Browser:
Opera 9.80 Opera 9.80


WWW
« Reply #4 on: February 07, 2010, 07:24:07 pm »

Of course it is a special sorting algorithm applied to file lists.
A standard LISTVIEW will always sort "File10" before "File2"
I think listview used by Explorer uses a callback function for getting the needed effect.

I'm not saying what you asked is not correct. I just said it is not the form generally used.

In any case I will think about it. I think I can intercept the sorting algorithm and do something.

Ciao
Eros
Logged

www.thinbasic.com | community.thinbasic.compsch.thinbasic.com
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
Eros Olmi
Administrator
Hero Member
*****

Karma: 92
Online

Gender: Male
Posts: 6319


thinBasic co-author

OS:
Windows XP Windows XP
Browser:
Opera 9.80 Opera 9.80


WWW
« Reply #5 on: February 07, 2010, 07:41:30 pm »

I Google around and as expected I found this is a common problem to many languages:
http://www.google.com/search?hl=en&source=hp&q=sorting+strings+with+numbers&aq=9&aqi=g10&oq=sorting+string

Someone call it "intuitive sorting"

A way could be to:
  • get the array (only string arrays)
  • create many subsets of it each composed by the strings having the same exact length
  • sort each subset as usual
  • recompose the whole array as a concatenation of sorted subsets

but .... what happen if file names are something like:
1.txt
1.1.txt
1.01.txt
1.001.txt
If sorted by size (like in file1.txt, file2.txt and file10.txt) you will get a completely different sorting because numerically file named "1.001.txt" should come first because it is the smallest number of the list.

Just brainstorming ...

« Last Edit: February 07, 2010, 07:50:43 pm by Eros Olmi » Logged

www.thinbasic.com | community.thinbasic.compsch.thinbasic.com
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
Michael Clease
Sr. Member
****

Karma: 24
Offline

Gender: Male
Posts: 1226


Format c:\

OS:
Windows XP Windows XP
Browser:
Firefox 3.6 Firefox 3.6


« Reply #6 on: February 07, 2010, 08:52:37 pm »


but .... what happen if file names are something like:
1.txt
1.1.txt
1.01.txt
1.001.txt
If sorted by size (like in file1.txt, file2.txt and file10.txt) you will get a completely different sorting because numerically file named "1.001.txt" should come first because it is the smallest number of the list.

Just brainstorming ...

The order you are showing is correct because it has nothing to do with what value but how many characters.

Logged

Why is the world full of nobs trying to push my buttons!!

Home Desktop : WinXP Pro SP3 - Intel Pentium (D)   - 3.0 Ghz - 2GB - Geforce 6800GS
Home Laptop   : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
Home Laptop   : WinXP Pro SP3 - Intel Pentium 4 - 3 Ghz - 512 MB - ATI Mobile Radeon 7000 IGP
Work Desktop  : WinXP Pro SP3 - Intel core 2 Quad Q8200 - 2.33 Ghz - 2 Gb - Quadro Fx 370
Eros Olmi
Administrator
Hero Member
*****

Karma: 92
Online

Gender: Male
Posts: 6319


thinBasic co-author

OS:
Windows XP Windows XP
Browser:
Opera 9.80 Opera 9.80


WWW
« Reply #7 on: February 07, 2010, 09:11:18 pm »

Do you think this kind of sorting can be applied only to files?

I mean, I was thinking about a special sorting algorithm working only on file names because it is not the string length that makes the different but the file name without extension.
In this can I can scan the string array, remove the file extension and work on file name length.
Than return an "intuitive" sorted array.
Logged

www.thinbasic.com | community.thinbasic.compsch.thinbasic.com
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
Eros Olmi
Administrator
Hero Member
*****

Karma: 92
Online

Gender: Male
Posts: 6319


thinBasic co-author

OS:
Windows XP Windows XP
Browser:
Opera 9.80 Opera 9.80


WWW
« Reply #8 on: February 07, 2010, 10:08:55 pm »

I have something to test for you Mike.

I've changed ARRAY SORT syntax to the following:
Code
ARRAY SORT ArrayVariable([StartIndex]) [FOR nElements] [,{ASCEND | DESCEND}] [, ASFILES]

so you can call it something like
Code
Array Sort sArray, AsFiles
Array Sort sArray, Ascend, AsFiles
Array Sort sArray, Descend, AsFiles
 

Here it is the way clause ASFILE works:
  • right search into array element for file extension and get it off
  • in the remaining part it right search for any numeric char in the range (0123456789.) and get it off
  • at this point we have 3 parts:
    • (A) left part of the string
    • (B) the numeric part (it can be missed)
    • (C) the extension of the file (it can be missed)
  • now the string is filled with NULLs ( CHR$(0) ) up to 32 bytes in the following way: (A) & NULLs & (B) & (C)
    The NULLs part is variable in length depending on how many numeric chars (B part) are present
    In few words the numeric part of the file name is normalized with NULLs up to a certain size in order to have all file names with the same size.
    For example:
    file1.txt   is transformed into file______1.txt
    file10.txt  is transformed into file_____10.txt
    file2.txt   is transformed into file______2.txt
    with _ be $NUL chars
  • array is sorted
  • NULLs are removed
  • sorted array is returned

Let me know if it works or there are cases where it creates problems.
Maybe I will change clause ASFILES to something ASNATURAL but for this tests just use ASFILES

Ciao
Eros

ATTENTION !!!! Attached thinCore.dll works only if you have thinBasic 1.7.10.1 installed otherwise do not install !!!!!!!



Updated 2009.02.08
Attached files
* thinCore.zip (131.34 KB - downloaded 8 times.)
« Last Edit: February 09, 2010, 12:31:58 am by Eros Olmi » Logged

www.thinbasic.com | community.thinbasic.compsch.thinbasic.com
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
Michael Clease
Sr. Member
****

Karma: 24
Offline

Gender: Male
Posts: 1226


Format c:\

OS:
Windows XP Windows XP
Browser:
Firefox 3.5.7 Firefox 3.5.7


« Reply #9 on: February 08, 2010, 10:45:49 am »

Thanks for all your hard work but it managed to break it

If you use this string in my script with the added "asfiles" it blanks the array

 Txt = "1,2,3,11,111,1111,11111,2222,22"

For syntax can I suggest
 
Code
ARRAY SORT ArrayVariable([StartIndex]) [FOR nElements] [,{ASCEND | DESCEND}] [, Full ]
 

the Full option being either %True or %False the default being %False because it will be faster I am guessing.
Logged

Why is the world full of nobs trying to push my buttons!!

Home Desktop : WinXP Pro SP3 - Intel Pentium (D)   - 3.0 Ghz - 2GB - Geforce 6800GS
Home Laptop   : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
Home Laptop   : WinXP Pro SP3 - Intel Pentium 4 - 3 Ghz - 512 MB - ATI Mobile Radeon 7000 IGP
Work Desktop  : WinXP Pro SP3 - Intel core 2 Quad Q8200 - 2.33 Ghz - 2 Gb - Quadro Fx 370
Eros Olmi
Administrator
Hero Member
*****

Karma: 92
Online

Gender: Male
Posts: 6319


thinBasic co-author

OS:
Windows XP Windows XP
Browser:
Microsoft Internet Explorer 8.0 Microsoft Internet Explorer 8.0


WWW
« Reply #10 on: February 08, 2010, 12:05:00 pm »

Mike,

ok, thanks.
In any case I think I'm close to a solution.
I will go on this evening when at home.

Eros
Logged

www.thinbasic.com | community.thinbasic.compsch.thinbasic.com
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
Eros Olmi
Administrator
Hero Member
*****

Karma: 92
Online

Gender: Male
Posts: 6319


thinBasic co-author

OS:
Windows XP Windows XP
Browser:
Microsoft Internet Explorer 7.0 Microsoft Internet Explorer 7.0


WWW
« Reply #11 on: February 08, 2010, 10:24:50 pm »

Mike,

I've updated thinCore.dll in above post here just above
http://community.thinbasic.com/index.php?topic=3196.msg23973#msg23973

I hope I've fixed it.
Let me know if you find other cases where it doesn't work.

Eros
Logged

www.thinbasic.com | community.thinbasic.compsch.thinbasic.com
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
Michael Clease
Sr. Member
****

Karma: 24
Offline

Gender: Male
Posts: 1226


Format c:\

OS:
Windows XP Windows XP
Browser:
Firefox 3.5.7 Firefox 3.5.7


« Reply #12 on: February 08, 2010, 11:31:43 pm »

Input Text = "AAA,AAB,AAC,AAAA,AABA,ABBA"

Code
Text Before Sort             Text After Sort
 
AAA                          AAA
AAB                          AAAA
AAC                          AAB
AAAA                         AABA
AABA                         AAC
ABBA                         ABBA

I would still say its not working but do we have the same goal?  in this example I would expect the output to match the input.

Logged

Why is the world full of nobs trying to push my buttons!!

Home Desktop : WinXP Pro SP3 - Intel Pentium (D)   - 3.0 Ghz - 2GB - Geforce 6800GS
Home Laptop   : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
Home Laptop   : WinXP Pro SP3 - Intel Pentium 4 - 3 Ghz - 512 MB - ATI Mobile Radeon 7000 IGP
Work Desktop  : WinXP Pro SP3 - Intel core 2 Quad Q8200 - 2.33 Ghz - 2 Gb - Quadro Fx 370
Eros Olmi
Administrator
Hero Member
*****

Karma: 92
Online

Gender: Male
Posts: 6319


thinBasic co-author

OS:
Windows XP Windows XP
Browser:
Microsoft Internet Explorer 7.0 Microsoft Internet Explorer 7.0


WWW
« Reply #13 on: February 09, 2010, 12:36:12 am »

Updated again above thinCore.zip that should take case also of latest example.

Yes we are going same direction but that direction seems more complex than expected.
Also I'm not sure I've used the best possible algorithm.
There are a lot of limitations in current algorithm regarding size of strings that will be possible to sort in this way.
I will talk more in future posts as soon as I will have some spare time.

Let me know if with latest Core there are still uncovered situations.

Ciao
Eros
Logged

www.thinbasic.com | community.thinbasic.compsch.thinbasic.com
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
Michael Clease
Sr. Member
****

Karma: 24
Offline

Gender: Male
Posts: 1226


Format c:\

OS:
Windows XP Windows XP
Browser:
Firefox 3.5.7 Firefox 3.5.7


« Reply #14 on: February 09, 2010, 12:52:51 am »

Thank you as always it seem to be working but I push it around at a more civlilised hour, the only other thing that is open to interpretation are the characters  !"£$%^&*()_=+ ... etc but it seems really good to me.

Thanks again.

Mike
Logged

Why is the world full of nobs trying to push my buttons!!

Home Desktop : WinXP Pro SP3 - Intel Pentium (D)   - 3.0 Ghz - 2GB - Geforce 6800GS
Home Laptop   : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
Home Laptop   : WinXP Pro SP3 - Intel Pentium 4 - 3 Ghz - 512 MB - ATI Mobile Radeon 7000 IGP
Work Desktop  : WinXP Pro SP3 - Intel core 2 Quad Q8200 - 2.33 Ghz - 2 Gb - Quadro Fx 370
Tags:
Pages: 1 2
  Add bookmark  |  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!


Google visited last this page August 24, 2010, 03:46:08 am