+ Reply to Thread
Results 1 to 36 of 36

Remove duplicates and sort array

Hybrid View

abousetta Remove duplicates and sort... 02-22-2012, 01:35 PM
Richard Buttrey Re: Most efficient way to... 02-22-2012, 02:09 PM
abousetta Re: Most efficient way to... 02-22-2012, 02:20 PM
Richard Buttrey Re: Most efficient way to... 02-22-2012, 02:27 PM
Leith Ross Re: Most efficient way to... 02-22-2012, 03:56 PM
abousetta Re: Most efficient way to... 02-22-2012, 04:09 PM
Leith Ross Re: Most efficient way to... 02-22-2012, 04:21 PM
abousetta Re: Remove duplicates and... 02-22-2012, 04:25 PM
abousetta Re: Remove duplicates and... 02-22-2012, 04:35 PM
snb Re: Remove duplicates and... 02-22-2012, 05:04 PM
abousetta Re: Remove duplicates and... 02-22-2012, 05:43 PM
snb Re: Remove duplicates and... 02-22-2012, 06:08 PM
abousetta Re: Remove duplicates and... 02-22-2012, 06:36 PM
snb Re: Remove duplicates and... 02-22-2012, 07:24 PM
abousetta Re: Remove duplicates and... 02-22-2012, 09:47 PM
mikerickson Re: Remove duplicates and... 10-11-2012, 01:47 AM
abousetta Re: Remove duplicates and... 02-24-2012, 02:18 AM
nilem Re: Remove duplicates and... 02-24-2012, 02:52 AM
abousetta Re: Remove duplicates and... 02-24-2012, 03:40 AM
nilem Re: Remove duplicates and... 02-24-2012, 04:23 AM
snb Re: Remove duplicates and... 02-24-2012, 04:06 AM
abousetta Re: Remove duplicates and... 02-24-2012, 10:14 AM
abousetta Re: Remove duplicates and... 02-25-2012, 03:18 AM
nilem Re: Remove duplicates and... 02-25-2012, 04:24 AM
DaveU Re: Remove duplicates and... 10-10-2012, 11:31 PM
jimmalk Re: Remove duplicates and... 10-11-2012, 12:42 AM
abousetta Re: Remove duplicates and... 02-25-2012, 05:29 AM
nilem Re: Remove duplicates and... 10-11-2012, 01:08 AM
DaveU Re: Remove duplicates and... 10-11-2012, 02:06 AM
abousetta Re: Remove duplicates and... 10-11-2012, 07:01 AM
mikerickson Re: Remove duplicates and... 10-11-2012, 09:11 AM
abousetta Re: Remove duplicates and... 10-11-2012, 09:24 AM
Kyle123 Re: Remove duplicates and... 10-11-2012, 09:36 AM
jindon Re: Remove duplicates and... 10-11-2012, 09:46 AM
Kyle123 Re: Remove duplicates and... 10-11-2012, 10:18 AM
jindon Re: Remove duplicates and... 10-11-2012, 10:22 AM
  1. #1
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Remove duplicates and sort array

    Hi,

    I have a one dimensional array and I want to remove all duplicates from the array so that each value in the array is unique. Then I need to sort all the 'unique' values in the array. What is the most efficient way to accomplish these tasks?

    Thanks.

    abousetta
    Last edited by abousetta; 02-24-2012 at 02:19 AM.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Most efficient way to remove duplicates and sort array

    Hi,

    Probably the simplest way is to use Data Filter Advanced Unique and copy to another range.
    Then sort the unique range.
    Delete your original array and cut and paste the unique range back to the top of the original array.

    Regards
    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Most efficient way to remove duplicates and sort array

    Hi Richard,

    Sorry I should have been more clear about the source of my array. It is a string converted into an array, actually two strings.

      ' Create arrays from strings
        Arr1 = Split(S1, " ")
        Arr2 = Split(S2, " ")
    
      ' Create one (common) array from the two arrays
        Arr3 = Split(Join(Arr1, "|") & "|" & Join(Arr2, "|"), "|")
        
      ' Remove duplicates from arrays
      '*
      '*
      '* Not coded yet
      '*
      '*
      '*
    What I am doing is comparing the two strings for similarities but there is no reason to go through repeated words since they will either be found or not.

    abousetta

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Most efficient way to remove duplicates and sort array

    Hi,

    Does Chip Pearson's article http://www.cpearson.com/excel/distinctvalues.aspx help.

    I'm sure what you want is in there somewhere.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Most efficient way to remove duplicates and sort array

    Hello Ahmed,

    I think a better approach would to be to use a Dictionary object. You can easily create a 1-D array of unique values. You could also do this with a Collection object. I prefer the Dictionary object as you can test if an element already exists without generating an error. If you need help coding this, let me know.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Most efficient way to remove duplicates and sort array

    Hi Leith,

    Hope all is well. An example would be great. I know that you posted some examples a while back when we were both responding to another thread where the OP wanted to remove duplicates from a large list, but I can't find the link to it.

    Thanks for all your help.

    abousetta

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Most efficient way to remove duplicates and sort array

    Hello Ahmed,

    Here is an example macro.
    
    Sub RemoveDupes()
    
        Dim Dict As Object
        Dim Key As String
        Dim Keys As Variant
        
          ' Create arrays from strings
            Arr1 = Split(S1, " ")
            Arr2 = Split(S2, " ")
    
          ' Create one (common) array from the two arrays
            Arr3 = Split(Join(Arr1, "|") & "|" & Join(Arr2, "|"), "|")
        
          ' Remove duplicates from arrays
          
            Set Dict = CreateObject("Scripting.Dictionary")
            
              ' Comment the next line if you want the matches to be case sensitive.
                Dict.CompareMode = vbTextCompare
                
                For Each Key In Arr3
                    Key = Trim(Key)
                    If Key <> "" Then
                       If Not Dict.Exists(Key) Then Dict.Add Key, 1
                    End If
                Next Key
                
           Erase Arr3
           Arr3 = Dict.Keys
        
    End Sub

  8. #8
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Perfect. Thanks Leith. I am trying to go the Array way these days (no pun intended). Arrays are said to be faster and more efficient and I am trying to learn to code using them but still learning the ropes.

    abousetta

  9. #9
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Hi Leith,

    I had to just make one small change to your code (Key as Variant instead of String), but it works great. Thanks a lot for your help.

    By any chance would you know how to solve the second half of the riddle (how to sort the array)? I am still searching but no obvious answer in sight.

    abousetta

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Remove duplicates and sort array

    Sub snb()
      sn= Split(S1)
      sp= Split(S2)
    
      sn = Split(Join(sn) & " " & Join(sp))
      for j=0 to ubound(sn)
        if instr(c01 & "|","|" & sn(j) & "|")=0 then c01=c01 & "|" & sn(j)
      next
    
    End Sub



  11. #11
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Thanks snb. Unfortunately, I can't seem to get it to work. The array seems to be empty (or contains null values) because even the Msgbox is not showing me anything within the newly created sn array. I will keep testing it and hopefully get it going.

    abousetta

  12. #12
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Remove duplicates and sort array

    Something has to be in S1 and S2.
    If not than use:

    Sub snb()
      sn= Split("aaaa sss ddd fff ggg hhh eee rrr")
      sp= Split("fff ggg hhh qwe wer erty rty")
    
      sn = Split(Join(sn) & " " & Join(sp))
      for j=0 to ubound(sn)
        if instr(c01 & "|","|" & sn(j) & "|")=0 then c01=c01 & "|" & sn(j)
      next
    End Sub

  13. #13
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Sorry. Now I know what I was doing wrong. c01 is a string. Therefore I needed to convert it back to an array to see the elements of the array.

    I am going to mark this thread as solved for now and work on trying to sort the elements within the array.

    Thanks everyone.

    abousetta

  14. #14
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Remove duplicates and sort array

    and now the sorting part: resulting in a sorted array 'sp'

    Sub snb()
      sn= Split("aaaa sss ddd fff ggg hhh eee rrr")
      sp= Split("fff ggg hhh qwe wer erty rty")
    
      sn = Split(Join(sn) & " " & Join(sp))
      for j=0 to ubound(sn)
        if instr(c01 & "|","|" & sn(j) & "|")=0 then c01=c01 & "|" & sn(j)
      next
    
      sn = Split(c01, "|")
      sp = Split(sn(0))
      c02 = sn(0)
        
      For j = 1 To UBound(sn)
       For jj = 0 To UBound(sp)
        If sn(j) < sp(jj) Then
         c02 = Replace(c02, sp(jj), sn(j) & "_" & sp(jj))
         Exit For
        End If
       Next
       If jj = UBound(sp) + 1 Then c02 = c02 & "_" & sn(j)
       sp = Split(c02, "_")
      Next
    end sub

  15. #15
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Thanks snb. As usual, I will need a big mug of coffee to try and decipher the advanced coding techniques, but I can always say that I am gaining a great amount of experience studying your approaches.

    abousetta

  16. #16
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Remove duplicates and sort array

    This does both in one routine

    Sub test()
        Dim delimitedString As String, Delimiter As String
        Dim AlteredString As String
        Dim SortedArrayOfNonDuplicates As Variant
        
        Delimiter = "|"
        delimitedString = "a|x|b|b|a|c|c|d|a|x|a|a"
        
        AlteredString = SortString(delimitedString, Delimiter)
        MsgBox AlteredString:  ' a|b|c|d|x
        
        SortedArrayOfNonDuplicates = Split(AlteredString, Delimiter)
    End Sub
    
    Function SortString(delimitedString As String, Optional Delimiter As String = " ")
        Dim strLeft As String, strRight As String
        Dim subStrings As Variant, oneSub As Variant
        Dim Pivot As String
        
        subStrings = Split(delimitedString, Delimiter)
        Pivot = subStrings(0)
        For Each oneSub In subStrings
            If oneSub < Pivot Then
                strLeft = strLeft & Delimiter & oneSub
            ElseIf Pivot < oneSub Then
                strRight = strRight & Delimiter & oneSub
            End If
        Next oneSub
        strLeft = Mid(strLeft, 2): strRight = Mid(strRight, 2)
        
        SortString = Pivot
        If strLeft <> vbNullString Then
            SortString = SortString(strLeft, Delimiter) & Delimiter & SortString
        End If
        If strRight <> vbNullString Then
            SortString = SortString & Delimiter & SortString(strRight, Delimiter)
        End If
    End Function
    Last edited by mikerickson; 10-11-2012 at 01:52 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  17. #17
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    I have modified the code that snb provided to compensate for the nature of the data, but have one final problem that I can't figure out.

          ' Remove duplicates from array
            For i = 0 To UBound(CitationArray)
                If InStr(NewString & "|", "|" & CitationArray(i) & "|") = 0 Then NewString = NewString & "|" & CitationArray(i)
            Next
            
          ' Remove first (empty) element from Array
            CitationArray = Split(NewString, "|")
            NewCitationArray = Split(CitationArray(0))
            EmptyString = CitationArray(0)
          
          ' Arrange elements in Array in alpaha-numeric order
            For A1 = 1 To UBound(CitationArray)
                For A2 = 0 To UBound(NewCitationArray)
                    If Len(EmptyString) = 0 Then EmptyString = CitationArray(A1)
                    If CitationArray(A1) < NewCitationArray(A2) Then
                        EmptyString = Replace(EmptyString, "_" & NewCitationArray(A2), "_" & CitationArray(A1) & "_" & NewCitationArray(A2))
                        Exit For
                    End If
                Next
                If A2 = UBound(NewCitationArray) + 1 Then EmptyString = EmptyString & "_" & CitationArray(A1)
                NewCitationArray = Split(EmptyString, "_")
            Next
    Now this works fine on small passages (a couple of sentences), but larger paragraphs the results are not accurate with some duplicates still being present and some out of alpha-numeric order. The weird thing is that when I rerun it again, it works just fine giving accurate results. For now I have put in a loop to go through the deduplicate and sort process twice, but this of course is inefficient.

    Any thoughts?

    abousetta

  18. #18
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Remove duplicates and sort array

    As an option. There is a NoDups ZVI's function. Here's an example using this function.
    Sub NoDupsTest()
    Dim i&, s$, j&, arr
    arr = Split("aaaa sss ddd fff ggg hhh aaaa eee rrr")
    On Error Resume Next
    With New Collection
        For j = LBound(arr) To UBound(arr)
            s = Trim(arr(j))
            If Len(s) > 0 Then
                If IsEmpty(.Item(s)) Then
                    For i = 1 To .Count
                        If s < .Item(i) Then Exit For
                    Next
                    If i > .Count Then .Add s, s Else .Add s, s, Before:=i
                End If
            End If
        Next
        ReDim arr(1 To .Count)
        For i = 1 To .Count
            arr(i) = .Item(i)
        Next
    End With
    [a1].Resize(, UBound(arr)).Value = arr
    End Sub

  19. #19
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Thanks nilem. I will test tomorrow and post back. Just out of curiosity what's a ZVI function (never came across this term before).

  20. #20
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Remove duplicates and sort array

    Quote Originally Posted by abousetta View Post
    ... Just out of curiosity what's a ZVI function (never came across this term before).
    ZVI - is the nickname of Vladimir Zakharov, superprofessional in programming, MVP Excel

  21. #21
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Remove duplicates and sort array

    If this is your goal:

    ' Remove first (empty) element from Array
    you should use
    CitationArray = Split(mid(NewString,2), "|")

    If you provide the string you are testing with we can have a look too.
    Last edited by snb; 02-24-2012 at 04:09 AM.

  22. #22
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Thanks nilem. I am unfortunately a fish out of water when it comes to Excel .

    In fact, I have heard of ZVI, but haven't ever studied his techniques... maybe its time to start.

    snb, I have uploaded a sample of ten rows that show the problem. In the first sheet, I ran the code for deduping and sorting once, while in the second sheet, I ran the code twice. As you can see, the second returns 100% for all three columns since they are all identical. All the code I used is in the upload (didn't strip it down since the problem may be lying somewhere else in the code that I don't see).

    Thanks.

    abousetta
    Attached Files Attached Files

  23. #23
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    nilem, the code works great! It does the deduping and sorting at the same time. I'm impressed

    Question though, what does the "&" sign after the variable mean?

    Dim i&, s$, j&
    Thanks again.

    abousetta

  24. #24
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Remove duplicates and sort array

    This is the abbreviation for types of variables:
    & means Long
    % Integer
    # Double
    $ String
    ! Single
    And I still give the original code of ZVI
    ' Function returns sorted one-dimensional array of unique values ​​of Rng range
    Function NoDups(rng As Range)
    Dim arr(), i&, s$, x
    ' Read data into the array, for convenience restrict the last line of the sheet data
    arr = Intersect(rng.Parent.UsedRange, rng).Value
    ' create a list
    On Error Resume Next
    With New Collection
        For Each x In arr()
            s = Trim(x)
            If Len(s) > 0 Then
                If IsEmpty(.Item(s)) Then
                    ' fast enough option to add value to the collection with sorting (from PGC01)
                    For i = 1 To .Count
                        If s < .Item(i) Then Exit For
                    Next
                    If i > .Count Then .Add s, s Else .Add s, s, Before:=i
                End If
            End If
        Next
        ' copy from the collection to an array
        ReDim arr(1 To .Count)
        For i = 1 To .Count
            arr(i) = .Item(i)
        Next
    End With
    'return an array
    NoDups = arr()
    End Function
    There is a faster way to extract unique values with sorting.

  25. #25
    Registered User
    Join Date
    12-29-2011
    Location
    Waldeck, Canada
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Remove duplicates and sort array

    Hi nilem,
    Thanks for this, just a question about this line:
    arr = Intersect(rng.Parent.UsedRange, rng).Value

    Why not just:
    arr = rng.Value

    I must be missing something,

    Thanks,

    Dave

  26. #26
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: Remove duplicates and sort array

    My compliments to everyone on this thread. Great stuff. Thank you

  27. #27
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Thanks nilem. I found this great thread and discussion between ZVI and pgc01. I am going to test the two three approaches (Instr, Dict, Collection). From what I have read, the resutls with the Dictionary approach should prove to be the fastest, but I want to see what the differences will be with my data sets.

  28. #28
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Remove duplicates and sort array

    Hi, Dave
    You can select the entire column, for example Set rng = Range("A:B"), and with an expression of arr = rng.Value you will have 1,048,576 * 2 elements in your array.
    But the expression of arr = Intersect(rng.Parent.UsedRange, rng).Value restricts the selected columns only used range on the sheet.

  29. #29
    Registered User
    Join Date
    12-29-2011
    Location
    Waldeck, Canada
    MS-Off Ver
    Excel 2010
    Posts
    4

    Smile Re: Remove duplicates and sort array

    Quote Originally Posted by nilem View Post
    Hi, Dave
    You can select the entire column, for example Set rng = Range("A:B"), and with an expression of arr = rng.Value you will have 1,048,576 * 2 elements in your array.
    But the expression of arr = Intersect(rng.Parent.UsedRange, rng).Value restricts the selected columns only used range on the sheet.
    Hi nilem,

    Oh yes, of course, I hadn't considered that possibility (entire columns). Thanks for the reply and great explanation.

    Regards,

    Dave

  30. #30
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Thanks again nilem and Mike. Mike's approach in itself is ingenious as it opens up a whole new approach that I had never thought of, which is having a function keep calling itself (looping) until it completes the task. I used to think this can only be accomplished by a sub calling a function with Do... Loop while approach and Public variables.

    I'm going to mark this thread as solved for now and want to thank everyone for the wealth of knowledge in this discussion.

    abousetta

  31. #31
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Remove duplicates and sort array

    Quote Originally Posted by abousetta View Post
    Thanks again nilem and Mike. Mike's approach in itself is ingenious as it opens up a whole new approach that I had never thought of, which is having a function keep calling itself (looping) until it completes the task. I used to think this can only be accomplished by a sub calling a function with Do... Loop while approach and Public variables.

    I'm going to mark this thread as solved for now and want to thank everyone for the wealth of knowledge in this discussion.

    abousetta
    A function that calls itself is called a "recursive function".

  32. #32
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Remove duplicates and sort array

    Thanks Mike. I currently remove duplicates with the scripting dictionary and then sort using one of the standard vb methods (e.g. bubble sort, etc.).

  33. #33
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Remove duplicates and sort array

    Why not use one object to sort and remove duplicates? Consider:
    Sub kyle()
    Dim a, s
    a = Array("sdf", "234", "sfdf", "sdf", "123")
    s = UniqueSort(a, True)
    End Sub
    
    Public Function UniqueSort(arr As Variant, Optional SortDesc As Boolean) As Variant
    Dim x As Long
        With CreateObject("system.collections.arraylist")
            For x = LBound(arr) To UBound(arr)
                If Not .contains(CStr(arr(x))) Then .Add arr(x)
            Next x
            .Sort
            If SortDesc Then .Reverse
            UniqueSort = .toarray
        End With
    End Function
    Last edited by Kyle123; 10-11-2012 at 09:41 AM.

  34. #34
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Remove duplicates and sort array

    See the disadvantage of use of arraylist/sortedlist.
    Sub kyle()
    Dim a, s
    a = Array("sdf", "234", "sfdf", "sdf", "123", "2")
    s = UniqueSort(a, True)
    MsgBox Join(s, vbLf)
    End Sub
    
    Public Function UniqueSort(arr As Variant, Optional SortDesc As Boolean) As Variant
    Dim x As Long
        With CreateObject("system.collections.arraylist")
            For x = LBound(arr) To UBound(arr)
                If Not .contains(CStr(arr(x))) Then .Add arr(x)
            Next x
            .Sort
            If SortDesc Then .Reverse
            UniqueSort = .toarray
        End With
    End Function

  35. #35
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Remove duplicates and sort array

    True, but that's only a limitation of mixed types. For all strings or all numbers, it is perfectly valid - still something to bear in mind

  36. #36
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Remove duplicates and sort array

    Quote Originally Posted by Kyle123 View Post
    True, but that's only a limitation of mixed types. For all strings or all numbers, it is perfectly valid - still something to bear in mind
    That is critical....

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1