A simple sort problem?

Just starting out? Need help? Post your questions and find answers here.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

A simple sort problem?

Post by collectordave »

I have a list of strings in the form

1
2A
2
3
20

The numeric parts can be up to 99999 and the alpha part can be any letter a to z

The list can be in any order and I need to sort the list so the desired output would be:-

1
2
2A
3
20

tried this:-

Code: Select all


Global NewList MyStrings.s()

AddElement(MyStrings())

MyStrings() = "1"

AddElement(MyStrings())

MyStrings() = "2A"

AddElement(MyStrings())

MyStrings() = "2"

AddElement(MyStrings())

MyStrings() = "3"

AddElement(MyStrings())

MyStrings() = "20"

ForEach MyStrings()
  
  Debug MyStrings()
  
Next

Debug ""

SortList(MyStrings(),#PB_Sort_Ascending)

ForEach MyStrings()
  
  Debug MyStrings()
  
Next
Is there any way to sort this list?

Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: A simple sort problem?

Post by Keya »

Im guessing the existing sort routines won't work specifically for how you need them.

How about creating a duplicate array, but modifying them so that, for example...
1 = 10000
2 = 20000
2A = 20256 (A = +256.... B = +257 etc) (or just +use their Asc() values)

Please forgive me, my numbers are off, but I hope you get the general idea
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: A simple sort problem?

Post by Marc56us »

Interesting problem 8)
I thought I had a solution, but no, it doesn't work because the sort order is not "logical".
I give my solution anyway in case it gives ideas to the following ones ?
(extract last char, convert to string, fill with left "0", then remove)
The numeric parts can be up to 99999 and the alpha part can be any letter a to z

Code: Select all

Global NewList MyStrings.s()

AddElement(MyStrings()) : MyStrings() = "1"
AddElement(MyStrings()) : MyStrings() = "2A"
AddElement(MyStrings()) : MyStrings() = "2"
AddElement(MyStrings()) : MyStrings() = "3"
AddElement(MyStrings()) : MyStrings() = "20"

ForEach MyStrings()
    Debug MyStrings()
Next

ForEach MyStrings()
    Letter$ = Right(MyStrings(), 1)
    Number$ = RemoveString(MyStrings(), Letter$)
    MyStrings() = LTrim(RSet(Number$, 5, "0") + Letter$, "0")
Next

Debug ""

SortList(MyStrings(),#PB_Sort_Ascending)

ForEach MyStrings()
    Debug MyStrings()
Next
Result:

Code: Select all

1
2
20
2A
3
Expected:

Code: Select all

1
2
2A
3
20
Actually I think I forgot to test that the last character is a letter before removing it, so the eventual zero goes too

:)
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: A simple sort problem?

Post by collectordave »

Thanks for the replies.

Given me an idea.

If I pack the number with leading zeros so that

1 becomes 00001

and 20 becomes 00020 etc

it seems to work
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: A simple sort problem?

Post by collectordave »

Yes

got it working.

Pack with leading zeros and add on the alpha character.

Sort the list

Then remove the leading zeros.

Code: Select all

ForEach MyStrings()
  
  Alpha.s = Right(MyStrings(),Len(MyStrings()) - Len(Str(Val(MyStrings()))))

  MyStrings() =  RSet(Str(Val(MyStrings())),5,"0") + Alpha

Next

SortList(MyStrings(),#PB_Sort_Ascending)

ForEach MyStrings()
  
  For iLoop = 1 To 5
    
    If Left(MyStrings(),1) = "0"
      MyStrings() = Right(MyStrings(),Len(MyStrings()) -1)
    Else

      Break
    EndIf
    
  Next
  

  Debug MyStrings()
  
Next
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: A simple sort problem?

Post by Keya »

glad to hear you've got it going :) I would still opt for an integer version over string manipulation though (though maybe it's not worth it)
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: A simple sort problem?

Post by eck49 »

@collectordave

Good solution.
If at some point you need to extend the alpha part you could also pack on the right and your choice of packing could dictate the order of the strings

2A
2AA
2AC
2B
2DB

as you may want all single letters to come before double letters (cf usual spreadsheet column label notation) or just alphabetical like the above.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
User avatar
glomph
User
User
Posts: 48
Joined: Tue Apr 27, 2010 1:43 am
Location: St. Elsewhere / Germany
Contact:

Re: A simple sort problem?

Post by glomph »

Leading zeros are not necessary, spaces work too. To get rid of them, just use Trim().
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: A simple sort problem?

Post by Marc56us »

Simplify the end

Code: Select all

ForEach MyStrings()
;     For iLoop = 1 To 5 
;         If Left(MyStrings(),1) = "0"
;             MyStrings() = Right(MyStrings(),Len(MyStrings()) -1)
;         Else
;             Break
;         EndIf 
;     Next
    Debug LTrim(MyStrings(), "0")
Next
:wink:
Post Reply