Sort Structure by different values ???

Just starting out? Need help? Post your questions and find answers here.
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Sort Structure by different values ???

Post by Wolfram »

How can I sort a structure List by more than one item. I want to have sort it by first name and if some people have the same first name
they should by sort by last name.

Example:
How can I sort this List that I get "Anton, Smith" "Paul, Amason" "Paul, Simon"

Code: Select all

Structure userStruc
  firstName.s
  lastName.s
EndStructure

NewList user.userStruc()

AddElement(user())
user()\firstName ="Paul"
user()\lastName ="Simon"

AddElement(user())
user()\firstName ="Paul"
user()\lastName ="Amason"

AddElement(user())
user()\firstName ="Anton"
user()\lastName ="Smith"

macOS Catalina 10.15.7
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Sort Structure by different values ???

Post by Demivec »

Code: Select all

Structure userStruc
  firstName.s
  lastName.s
EndStructure

NewList user.userStruc()

AddElement(user())
user()\firstName ="Paul"
user()\lastName ="Simon"

AddElement(user())
user()\firstName ="Paul"
user()\lastName ="Amason"

AddElement(user())
user()\firstName ="Anton"
user()\lastName ="Smith"

;sort in the reverse ordering of the keys, i.e. first key = lastName, second key = firstName, etc.
SortStructuredList(user(), #PB_Sort_Ascending, OffsetOf(userStruc\firstName), #PB_String)
SortStructuredList(user(), #PB_Sort_Ascending, OffsetOf(userStruc\lastName), #PB_String)

ForEach user()
  Debug user()\lastName + ", " + user()\firstName
Next
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Re: Sort Structure by different values ???

Post by Wolfram »

This does't work.
You sort by firstname and than you make a new sort by lastname.

I need this result.
"Anton, Smith"
"Paul, Amason"
"Paul, Simon"
macOS Catalina 10.15.7
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Sort Structure by different values ???

Post by Demivec »

Wolfram wrote:This does't work.
You sort by firstname and than you make a new sort by lastname.

I need this result.
"Anton, Smith"
"Paul, Amason"
"Paul, Simon"
I gave a general solution so that you could adapt it not only to this problem but to any in the future.

Notice this comment in the code:

Code: Select all

;sort in the reverse ordering of the keys, i.e. first key = lastName, second key = firstName, etc.
SortStructuredList(user(), #PB_Sort_Ascending, OffsetOf(userStruc\firstName), #PB_String)
SortStructuredList(user(), #PB_Sort_Ascending, OffsetOf(userStruc\lastName), #PB_String)
You apparently wanted: first key = firstName, second key = lastName.
This means you wanted things sorted by 'firstName', then when those match, by 'lastName'. They can individually be either in ascending or descending order, however you need it. If you needed further sorting by additional keys, and they were part of the same structure, you could do any of those as well as a third key, fourth key etc..

You would thus use:

Code: Select all

SortStructuredList(user(), #PB_Sort_Ascending, OffsetOf(userStruc\lastName), #PB_String) ;second key
SortStructuredList(user(), #PB_Sort_Ascending, OffsetOf(userStruc\firstName), #PB_String) ;first key

ForEach user()
  Debug user()\firstName + ", " + user()\lastName
Next
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: Sort Structure by different values ???

Post by said »

If what you are interested in is a multi-field sorting of a structured array, i have created in the past something for this you can have a look at that link, it can be of some help :D

http://www.purebasic.fr/english/viewtop ... 12&t=63585
Post Reply