Jump to content

Complicated Sort for multiple arrays

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Greelmo

Greelmo

    Newbie

  • Members
  • Pip
  • 6 posts
Hello everyone,

This is my premiere post here, and I look forward to hearing from you all.

I'm currently developing a web application and am stuck at a very complicated spot. Here's the problem:

I have events that I'm pulling from a database and putting on a table. There are arrays that represent each attribute of the events (i.e. $Starttime is an array of all the start times). I have 6 or so arrays like that. So for event 1, $Starttime[1], $Endtime[1], etc. is put on the table. I hope that makes sense.

Now, I want to be able to sort these events by any and all of the variables. I'm not sure how to sort one array and maintain the key. So if I sort for $Starttime, $Starttime[1] might not correspond to $Endtime[1] anymore. How can I keep all the events related post-sort?

What is the best strategy for this?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It sounds like you need an array of pairs of times, rather than two arrays of times.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Greelmo

Greelmo

    Newbie

  • Members
  • Pip
  • 6 posts
Hey Panther,

I believe that would be the solution if i were to only sort by time. However, there's a category attribute, a distance attribute, etc. all under that same key system and I want to be able to sort by each attribute independently.

Thanks,

-Greelmo

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What I would do is have an array of tuples. Then, have a modified sort routine that will allow you to select which item in the tuples to use as the sort key.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog