Announcement

Collapse
No announcement yet.

Sort 2-dimensional array

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sort 2-dimensional array

    I need to sort five price values and keep track of the labels associated with those prices so that I know what the values represent when I'm done. I need to do both ascending and descending sort depending upon varying conditions.

    Rather than re-invent the wheel, I was hoping someone had a nice sort routine for two-dimensional arrays they would be willing to share.

    If there is an easier way than using a two-dimensional array, I'm open to ideas.


    Thanks

  • #2
    Hello Gavishti,

    Try doing a search using the following phrase, sort AND array*. There have been several threads discussing the sorting of array lately. This should help you find what you need. Look for Steve Hare's (stevehare2003) code examples.
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      Gavishti:

      I typically use an array of objects, which would be the equivalent of a struct in C or a Record in Delphi. See attached script.

      Chris
      Attached Files

      Comment


      • #4
        Thanks Chris. I like structs. The efs this is going into has a lot going on and I'm trying to squeeze out every last ounce of speed. From what I've read in the JS manual I get the impression objects are not as efficient as multi-dimensional arrays. Do you have any information about that? I'd sure rather use objects. Yes, I saw that an array is an object. Difference being that it is a pre-defined object.

        Comment


        • #5
          Gavishti:

          I use a lot of fairly involved scripts that contain standard arrays, arrays of objects or both and I have not really noticed a performance bias between standard arrays and arrays of objects. I have noticed a significant difference (obviously) between scripts that don't use arrays and those that do. I do all of my coding and testing on an ancient machine (500Mhz) for that very reason...I figure that if it works well on my system it should fly on a real computer.

          Even if there is a way to effectively handle the sorting of multi-dimensional arrays (and there probably is) I would still stick with the array of objects approach simply because it is a little more organized and it translates well from C or Delphi (e.g., the structs and records). When I work with multi-dimensional arrays, my head starts to hurt if I dimension past 2 or 3.... trying to keep track of what goes where when debugging is a pain, at least for me

          Chris

          Comment


          • #6
            When I have to deal with multi-dimensional arrays or multiple arrays going in different directions I get brain-melt also. Steve Hare's trace() idea helps with the debugging.

            Comment


            • #7
              Yes, Steve did a great job with that!

              Here is a link I found regarding sorting multi-dimensional arrays in Javascript. Just reading it makes my head hurt but it looks like it will do the trick.

              http://www.kryogenix.org/code/browser/sorttable/

              Chris

              Comment


              • #8
                Turns out that what I want to do is a lot simpler than expected. See attached test efs.
                Attached Files

                Comment

                Working...
                X