Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. String sorting

String sorting

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmsdata-structuresquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Erik Hammar
    wrote on last edited by
    #1

    Is there any fast function for sorting an array of strings alfabetically ? Thanks in advance. Erik

    T 1 Reply Last reply
    0
    • E Erik Hammar

      Is there any fast function for sorting an array of strings alfabetically ? Thanks in advance. Erik

      T Offline
      T Offline
      Tim Deveaux
      wrote on last edited by
      #2

      I can't think of a single call you can make, but there are lots of 'points of interest'. First tip, don't sort an array of strings - sort a contiguous array of pointers to the strings. This means whatever shifting you do just involves moving bits of the array around, not reallocating strings. With that in place, there are standard sorting routines you can use. The venerable qsort from the C library works great, though it requires you to set up a 'global' callback. Maybe this could be a static member in C++ if you want to mix. Inside the comparison callback, its up to you to decide what the order will be - retuning essentially what a strcmp or stricmp returns. It turns out that there is more than one implementation for the string comparison functions in VC - there is straight C code, and there is much faster 286 code in the Crt\Intel include dir. (I say '286 code' because I think some of this could be even faster with the string ops of a 386 + proc when dealing with big strings). If you've got control over what proc you are targeting, even a basic framework like this can be way fast, depending of course on the size and similarity of the strings. STL gurus will probably flame me for this, but it is durn quick! :)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups