Sort unique

Feature requests for future versions of Hollywood can be voiced here
Post Reply
User avatar
emeck
Posts: 169
Joined: Fri Apr 03, 2015 3:17 pm

Sort unique

Post by emeck »

Hello,

could an option be added to the Sort() function so duplicated entries are removed? Something like:

Sort(array, unique)

For example, I have this code:

Code: Select all

mainDB = {
                {category="Learning", name="Enrique", notes="Some notes.", modified="18-08-2022"},
                {category="Shop", name="Ricardo", notes="Another note.", modified="10-09-2022"},
                {category="Learning", name="Angel", notes="More notes.", modified="18-09-2022"},
                {category="Mail", name="Roger", notes="Note.",modified="01-05-2022"},
                {category="Accounts", name="Francisco", notes="Last note.", modified="18-09-2022"},
                {category="Accounts", name="Rafael", notes="What note.",modified="27-08-2022"},
                {category="Learning", name="Jordi", notes="My note.",modified="11-06-2022"},
              }

categoryList = {}

Function p_GetCategories()
    Local tempCategories = {}    
    
    ;save category from each record in temporary array
    For i = 0 To (TableItems(mainDB) - 1)
        Local item = (mainDB[i].category)
        InsertItem(tempCategories, item)
    Next

    ;sort temporary array
    Sort(tempCategories)

    ;save first record from temporary to final category array
    InsertItem(categoryList, tempCategories[0])

    ;compare remaining temporary records with the previous record
    ;in the final array and insert it if it's different
    For i = 1 To (TableItems(tempCategories)-1)
        If tempCategories[i] <> tempCategories[i-1] Then InsertItem(categoryList, tempCategories[i])
    Next
EndFunction

ForEachI(categoryList, DebugPrint)
The output is:

Code: Select all

0 Accounts
1 Learning
2 Mail
3 Shop
PowerBook 5.2 MorphOS 3.15
PowerBook 5.8 MorphOS 3.15
Amiga 1200 BPPC/BVision AOS4.1 FE
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Sort unique

Post by Bugala »

It's not a bad idea, I can see how that can be useful every now and then.

However, I'm not sure it should be added to the Sort() function itself, perhaps instead some other command, like RemoveDuplicates(), or rather even some other thing that would include an option to remove duplicates.

This way you would first Sort() a list, and then RemoveDuplicates() the result, and end in the result you are looking for.
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: Sort unique

Post by SamuraiCrow »

Removing duplicates is normally accomplished by making a reverse lookup table and checking if that has an entry for the current value using that as a key.
I'm on registered MorphOS using FlowStudio.
User avatar
emeck
Posts: 169
Joined: Fri Apr 03, 2015 3:17 pm

Re: Sort unique

Post by emeck »

@Bugala
Yes, maybe it would be cleaner having it as a separate function.

@SamuraiCrow
Can you explain how a reverse lookup table works for this?
PowerBook 5.2 MorphOS 3.15
PowerBook 5.8 MorphOS 3.15
Amiga 1200 BPPC/BVision AOS4.1 FE
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: Sort unique

Post by SamuraiCrow »

Here's a shorter version of your example:

Code: Select all

mainDB = {
    {category="Learning", name="Enrique", notes="Some notes.", modified="18-08-2022"},
    {category="Shop", name="Ricardo", notes="Another note.", modified="10-09-2022"},
    {category="Learning", name="Angel", notes="More notes.", modified="18-09-2022"},
    {category="Mail", name="Roger", notes="Note.",modified="01-05-2022"},
    {category="Accounts", name="Francisco", notes="Last note.", modified="18-09-2022"},
    {category="Accounts", name="Rafael", notes="What note.",modified="27-08-2022"},
    {category="Learning", name="Jordi", notes="My note.",modified="11-06-2022"},
}

categoryList = {}

Function p_GetCategories()
    Local tempCategories = {}
    
    ;add only unique categories to array
    ForEach(mainDB, Function(key, value)
        Local item=value.category
        If RawGet(tempCategories, item) = Nil
            tempCategories[item] = True
            InsertItem(categoryList, item)
        EndIf
    EndFunction)

    Sort(categoryList)

EndFunction

p_GetCategories()

ForEachI(categoryList, DebugPrint)
ListRequest("Categories","Select a category",categoryList)
In this case, a full reverse-lookup wasn't needed so I just assigned True to each unique category making it effectively a hash set data structure. A true reverse-lookup would have had an array of all the records in the database for each corresponding category in the tempCategories table.
I'm on registered MorphOS using FlowStudio.
User avatar
emeck
Posts: 169
Joined: Fri Apr 03, 2015 3:17 pm

Re: Sort unique

Post by emeck »

@SamuraiCrow

Ok, I understand it better now. Thanks, this will be useful.
PowerBook 5.2 MorphOS 3.15
PowerBook 5.8 MorphOS 3.15
Amiga 1200 BPPC/BVision AOS4.1 FE
Post Reply