473,386 Members | 1,842 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,386 software developers and data experts.

sort based on substring

A bit of a dumb question. I have an array of directories and would
like to sort the array based on the third directory element...

e.g., from...

here/there/200801/everywhere
here/there/200803/everywhere2
here/there/200802/everywhere3

to...

here/there/200801/everywhere
here/there/200802/everywhere2
here/there/200803/everywhere3

I can think of lots of unelegant ways, is there an elegant one? (few
lines of code?)

Thanks...
Jan 22 '08 #1
2 3077
On Tue, 22 Jan 2008 18:04:31 +0100, brechmos <br******@gmail.comwrote:
A bit of a dumb question. I have an array of directories and would
like to sort the array based on the third directory element...

e.g., from...

here/there/200801/everywhere
here/there/200803/everywhere2
here/there/200802/everywhere3

to...

here/there/200801/everywhere
here/there/200802/everywhere2
here/there/200803/everywhere3

I can think of lots of unelegant ways, is there an elegant one? (few
lines of code?)
function _mysort($a,$b){
$a_array = explode('/',$a);
$asorter = isset($a_array[2]) ? $a_array[2] : '';
$b_array = explode('/',$b);
$bsorter = isset($b_array[2]) ? $b_array[2] : '';
return strcmp($asorter,$bsorter);
}
usort($array,'_mysort');

--
Rik Wasmus
Jan 22 '08 #2
I knew there must be a good answer. Thanks.

>
function _mysort($a,$b){
$a_array = explode('/',$a);
$asorter = isset($a_array[2]) ? $a_array[2] : '';
$b_array = explode('/',$b);
$bsorter = isset($b_array[2]) ? $b_array[2] : '';
return strcmp($asorter,$bsorter);}

usort($array,'_mysort');

--
Rik Wasmus
Jan 22 '08 #3

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

Similar topics

1
by: Rolf Kemper | last post by:
Hi All, I'm quite sure that somebody of you have already a solution for this: ABC1 ABC12 ABC2 ABC21 ABC3
1
by: Denzil | last post by:
Hi, I am using the DataView.Sort Property (on a DataView column) to sort the View based on some condition. Now the column that I need to sort contains characters and HTML code that should not...
3
by: Ali Chambers | last post by:
Hi, I have a bit of a problem with a sort procedure I need to do. I have a list of items in a listbox, eg:- 2.3%<A other text here> -4%<B other text here> 10%<C other text here> -9.3%<D...
1
by: anonieko | last post by:
I have a directory of files and I want to sort in in Array List in a customized way. How do I do that? > > > > > > class main {
0
by: Chazam | last post by:
What I'm Trying to do is sort By Date. I came up whith the idea of splite the date in year, month and day then sort them individualy. this work perfect in xsl <xsl:sort order ="descending"...
0
by: DBLWizard | last post by:
I posted a message on the topic earlier and thought I had it solved but in testing have found problems. I have a Templated GridView control on a webpage bound to an ObjectDataSource. There are 3...
2
by: ajc308 | last post by:
I have an XML document that looks like the following: <root name="PlanRepository"> <directory name="connoraj"> <directory name="single_run1"> <file>insidebox.txt</file> ...
1
mfshake
by: mfshake | last post by:
i am using only a driver and not a server. I know how to do selection sort for ints but can't figure out how to do it for Strings. I want to sort it my first letter only Here is my code that i...
7
by: otis | last post by:
Hi all, This is a small issue to make things prettier, but we all know how important that can be! I had an xsl:if to check if a node was the last one in a collection of nodes and if it was a...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...

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.