View Full Version : unusual sorting of array.
I want to list an array in a specific order where it is not alphabetical or sorted by any other method (that I am aware of).
The pages might be Home page, Facilities, Dining, Bars, Lounge, Conference Facilities, Location, Room Tariffs, News. The output for a specific business may mean that some or others, of those are ommitted.
I cant list them by date created as then when they are amended, they will move to a differnet order.
So, how could I force them to display in my chosen order?
Bazz
Aradon
06-29-2005, 02:24 PM
The best way (and the java way too but it should work here as well) is to just write your own comparing method and work off it.
There are several sorts out there (Insertion is the first that comes to mind) where you can modify them to your needs. For example if you compare two objects in your sort, you would compare them by calling another method that would then look at the two objects and say which one is greater. If the first object is greater return 1, if it's less return -1. You can then use these numbers in the method it called to use the insertion sort the way it was intended.
There are several examples of insertion sort around the web as well as other, more effiecnient sorts. Google works well in that case.
Thanks Aradon, I'll google insertion sort and see what I get. I had read the perl docs on 'sort' but I didn't see one that looked as though it would do as I need.
Bazz
Jeff Mott
06-29-2005, 03:51 PM
The pages might be Home page, Facilities, Dining, Bars, Lounge, Conference Facilities, Location, Room Tariffs, News.So you have an array of these things? And you want them to display in that particular order? And I'm guessing the array can come out in any order?
You said some can be omitted. Is what you have listed there the most options that could exist?
OK this is hard for me to explain.... but here goes.
As files are created in separate categories (Directories), through my cms, they are automatically added to the 'menu' on the outputted web page.
The first three (as is presently), are in the dir General_Pages. The rest are in their own-category directory (and sub, Nearby, News, Rooms and Special Offers) and are therefore outside of this sort function.
I need the About Us page to be called Home but if I do so presently, it won't remain at the top where it must be.
If, any other pages were to be added, later they would be of lesser importance than the first three and could go below these three in alphabetical order.
I was hoping that if I could understand the 'sort' function, that I could amend it as the content develops.
Pointers, help, would be greatly appreciated.
Bazz
edited: I considered 'substitution' but it would lead to clients confusion over what their files are called.
Jeff Mott
06-29-2005, 09:10 PM
You could do something like...my %important_things;
$important_things{$_} = 1 for qw[important_thing_1 important_thing_2 important_thing_3];
if (not $important_things{$a} ^ $important_things{$b}) {
# they're either both important or both unimportant, sort alphabetically or however
return $a cmp $b;
}
elsif ($important_things{$a}) {
# $a is important and $b is not, sort $a first regardless of their alphabetic order
return -1;
}
else {
# ditto, but vica versa
return 1;
}
### NOTE: code not tested ###
But in truth, it sounds more like the order you want them to appear is based on plain and simply good judgement. It might be better to maintain some kind of DB file that records the order that each item should appear.
Thank you for that, Jeff. I'll take a closer look at it and see if it suits my app.
Had a thought over night that might mean the solution is simpler than I had imagined. Instead of putting the homepage and location and facilities etc, in one dir, I could separate them like I did for the group of 'nearby' files. Then they should list (hopefully), in the order in which I run the sub routines.
Bazz
Edit.. yay that did it. possibly not the most efficient of code since it involves another sub with about 60 lines of code but, at least it works and it's within my scope as a perl coder of minimal competence.
Thanks Jeff.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.