473,770 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1387
_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.i t> 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(cmp Name); // 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(pro pName) {
return function(v1,v2) {
return cmp(v1[propName],v2[propName]);
}
}

$array.sort(cmp Property("name" )); // sorted by name property
$array.sort(cmp Property("numbe r")); // sorted by number property
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.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
10181
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 code... TCHAR myArray; DoStuff(myArray);
8
3350
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 || operator is before the && operator? e,g, In an expression like a = (z>x) || (x<0) && (z-y>9); is it guaranteed that z>x will be checked first?
26
4884
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 about the syntax of calling the same. #include <stdio.h> void fp1()
12
6253
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 to zero with a single statement. For example main()
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.