Page 1 of 1

Sort unique

Posted: Sun Sep 18, 2022 11:07 pm
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

Re: Sort unique

Posted: Mon Sep 19, 2022 11:41 am
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.

Re: Sort unique

Posted: Mon Sep 19, 2022 5:07 pm
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.

Re: Sort unique

Posted: Wed Sep 21, 2022 6:07 pm
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?

Re: Sort unique

Posted: Thu Sep 22, 2022 12:10 pm
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.

Re: Sort unique

Posted: Thu Sep 22, 2022 5:33 pm
by emeck
@SamuraiCrow

Ok, I understand it better now. Thanks, this will be useful.