Sort array w/ custom alphabet [a-z + æøå]

Ramachandran

New Member
Joined
Oct 17, 2011
Messages
47
I wish to sort an array of strings alphabetically including the nordic characters Æ, Ø, Å.
Nordic alphabet = "abcdefghijklmnopqrstuvwxyzæøå"

Most sorting algorithms I've come across (bubble, quick, merge) sorts by checking if "bb" > "ba".
which works well for "01...89abc..xyz"

However, "æ" > "å" and "ø" > "å"
which gives me the sorting, abc...xyzåæø.

Any tips on how to get around this? to get an efficient sorting algo that puts "bø" ahead of "bå" ?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
asc("å") = 229
asc("æ") = 230
asc("ø") = 248

One very crude way would be to replace "å" with an ascii > 248, but would prefer a more efficient way
 
Upvote 0
Hi

Create a custom list with your alphabet and then use it in the Sort (when you sort, in the Order combobox you can select your custom list).
 
Upvote 0
Hi

Create a custom list with your alphabet and then use it in the Sort (when you sort, in the Order combobox you can select your custom list).

Thanks for the reply, however I wish to sort a VBA array (which I'll then feed into a combobox).

Here's a code example:

Code:
Sub test()
    Dim nameArr()
    Dim i As Integer, L As String
    
    nameArr = Array("Albert", "Bill", "Øyvind", "Åse")
     
    BubbleSort nameArr
    
    For i = LBound(nameArr) To UBound(nameArr)
        L = L & vbCrLf & nameArr(i)
    Next
    
    MsgBox L
     
End Sub

Sub BubbleSort(MyArray() As Variant)
    Dim First As Integer
    Dim Last As Integer
    Dim i As Integer
    Dim j As Integer
    Dim Temp As String
    Dim List As String
    First = LBound(MyArray)
    Last = UBound(MyArray)
    For i = First To Last - 1
        For j = i + 1 To Last
            If MyArray(i) > MyArray(j) Then
                Temp = MyArray(j)
                MyArray(j) = MyArray(i)
                MyArray(i) = Temp
            End If
        Next j
    Next i
    For i = 1 To UBound(MyArray)
        List = List & vbCrLf & MyArray(i)
    Next
End Sub

This gives the output:
Code:
Albert
Bill
Åse
Øyvind

Where as I'd like it to produce:
Code:
Albert
Bill
Øyvind
Åse
 
Upvote 0
I see.

If you want to do it in vba, then I think your solution of replacing "å" with an ascii > 248 is not that crude.

You do the replace in the array, call the bubble sort and then replace back. Doesn't seem inefficient, it's a very simple solution.
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,873
Members
449,056
Latest member
ruhulaminappu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top