473,378 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

how to order an array by a particular rule or a function?

I'd like to roder an array:
$array[id][name]
$array[id][number]
$array[id][value]

I'd like to order $array by name or by numbre of by a
function($array[id1],$array[id2]) that return 1 or -1 if id1 is less tha
id2 or viceversa...
How can I do that?
How can I order in a complex case like this?
Thank you in advance,
Andrea.
Jul 23 '05 #1
2 1372
_andrea.l wrote:
I'd like to roder an array:
$array[id][name]
$array[id][number]
$array[id][value]
Is this an array? How is it created, and its values assigned?
I'd like to order $array by name or by numbre of by a
function($array[id1],$array[id2]) that return 1 or -1
if id1 is less tha id2 or viceversa...
What are id1 and id2, and in what sense do they have magnitudes?
How can I do that?
You have not provided sufficient information to have that question
answered.
How can I order in a complex case like this?

<snip>

Nothing presented so far suggest a complex case here.

Richard.
Jul 23 '05 #2
"_andrea.l" <an******************@libero.it> writes:
I'd like to roder an array:
$array[id][name]
$array[id][number]
$array[id][value]

I'd like to order $array by name or by numbre of by a
function($array[id1],$array[id2]) that return 1 or -1 if id1 is less tha
id2 or viceversa...


I assume "$array" is an array, and "id" refers to number indices.

Then you can sort using the "sort" method of arrays, which takes an
optional comparison function:
---
function cmpName(v1,v2) {
if (v1.name < v2.name) { return -1; }
else if (v1.name > v2.name) { return 1; }
else {return 0;}
}

$array.sort(cmpName); // now sorted by name
---

More generally, you can use these functions:
---
/* Compares anything that is recognized by "<" */
function cmp(v1,v2){
return (v2<v1)-(v1<v2); // tricky code, but it works
}

/* creates a function that compares objects by ther "propName" property */
function cmpProperty(propName) {
return function(v1,v2) {
return cmp(v1[propName],v2[propName]);
}
}

$array.sort(cmpProperty("name")); // sorted by name property
$array.sort(cmpProperty("number")); // sorted by number property
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: der | last post by:
Hello all, I've a question about order of evaluations in expressions that have && and || operators in them. The question is: will the evalution go left-to-right, no matter what -- even if the...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
12
by: raghu | last post by:
Hello I am working on a project where code optimization plays a vital role to get the required performance. I would like to know is there any possible to reinitialization an array of big size...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.