# String.prototype.localeCompare() without native code?

**URL:** <https://discourse.elm-lang.org/t/string-prototype-localecompare-without-native-code/1090>\
**Category:** Learn\
**Created:** [April 7, 2018, 6:03pm UTC](https://discourse.elm-lang.org/t/string-prototype-localecompare-without-native-code/1090 "2018-04-07T18:03:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/sebn/32/829_2.png) [@sebn](https://discourse.elm-lang.org/u/sebn)\
**Post date:** [April 7, 2018, 6:03pm UTC](https://discourse.elm-lang.org/t/string-prototype-localecompare-without-native-code/1090/1 "2018-04-07T18:03:32Z")

</div>

Hello there!

In some application, I’m currently using native code to wrap `String.prototype.localeCompare` in order for a model name starting with an accent like “É” not to end up after a name starting with a “V” for example.

Using a port doesn’t really makes sense to me (please correct me if I’m wrong).  
I assume the best way would be to reimplement locale-based sorting in some localization package, but I have no idea of the amount of work required to do so…

Does this already exist somewhere?  
Or is somebody already planning to implement it in its own package?

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![Dan\_Abrams](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dan_abrams/32/3171_2.png) [@Dan\_Abrams](https://discourse.elm-lang.org/u/Dan_Abrams)\
**Post date:** [April 7, 2018, 6:38pm UTC](https://discourse.elm-lang.org/t/string-prototype-localecompare-without-native-code/1090/2 "2018-04-07T18:38:40Z")

</div>

update: does this do the trick? [http://package.elm-lang.org/packages/mcordova47/elm-natural-ordering/latest](http://package.elm-lang.org/packages/mcordova47/elm-natural-ordering/latest)

---

<div class="post-metadata">

**Author:** ![sebn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/sebn/32/829_2.png) [@sebn](https://discourse.elm-lang.org/u/sebn)\
**Post date:** [April 7, 2018, 7:19pm UTC](https://discourse.elm-lang.org/t/string-prototype-localecompare-without-native-code/1090/3 "2018-04-07T19:19:23Z")

</div>

I just switched the native code with `NaturalOrdering.compare` and everything seems to work as expected.

Thanks @Dan_Abrams!

---

<div class="post-metadata">

**Author:** ![Hector](https://avatars.discourse-cdn.com/v4/letter/h/43a26b/32.png) [@Hector](https://discourse.elm-lang.org/u/Hector)\
**Post date:** [April 11, 2018, 3:23am UTC](https://discourse.elm-lang.org/t/string-prototype-localecompare-without-native-code/1090/4 "2018-04-11T03:23:10Z")

</div>

This package doesn’t take locales into account at all. It just removes diacritics for comparing. Maybe it does the trick for the OP but in any case it would be interesting to know how to interface with this function from Elm.

---

<div class="post-metadata">

**Author:** ![sebn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/sebn/32/829_2.png) [@sebn](https://discourse.elm-lang.org/u/sebn)\
**Post date:** [April 11, 2018, 7:01am UTC](https://discourse.elm-lang.org/t/string-prototype-localecompare-without-native-code/1090/5 "2018-04-11T07:01:04Z")

</div>

@Hector Definitely! (although I’m quite happy with the current solution)

---

<div class="post-metadata">

**Author:** ![mcordova47](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mcordova47/32/559_2.png) [@mcordova47](https://discourse.elm-lang.org/u/mcordova47)\
**Post date:** [April 11, 2018, 11:26am UTC](https://discourse.elm-lang.org/t/string-prototype-localecompare-without-native-code/1090/6 "2018-04-11T11:26:06Z")

</div>

I think the way you’d interface with the function depends on your use case. If you wanted to sort a list, it might be reasonable to send the list through a port and sort it using localeCompare, then send back the sorted list and store it in your model.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [May 11, 2018, 11:40am UTC](https://discourse.elm-lang.org/t/string-prototype-localecompare-without-native-code/1090/7 "2018-05-11T11:40:09Z")

</div>

Closed on May 11, 2018, 11:40am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
