KBD

Keith Devens .com

Thursday, November 20, 2008 Flag waving
"It's no trick for talented people to be interesting, but it's a gift to be interested. We want... – Randy S. Nelson (dean of Pixar University)
← Miller on LenoSimon builds an RSS aggregator →

Daily link icon Friday, April 4, 2003

Sort indices instead of data

You may have noticed my new blogroll on the left. That's generated straight from my list of feeds produced by my new RSS aggregator, NewsDesk. The source OPML file for that is at /mySubscriptions.opml (that seems to be the standard place for these things).

As you can see, the OPML file isn't sorted either by category or by feed name within the category. It would be nice if it were sorted (it's sorted within the program), but whatever, life isn't perfect. You'll notice that the blogroll on my left is sorted by category, and the feeds within each category are also sorted. So, I had to write code to sort that.

Sorting this OPML file isn't as simple as sorting a list of numbers. You can do that easily with built in sort routines. To sort my blogroll, I used a trick I've used before. You sort the indices, but leave the data alone.

I use my XML parser (part of my XML-RPC library) to parse the OPML file. Anyway, I'm not sure how to explain it much more beyond just showing code:

<?php

$cms
->incLibrary('xml');

$c = &file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/mySubscriptions.opml');
$b = &XML_unserialize($c);

$cat = &$b['opml']['body']['outline'];
$cat_count count_numeric_items($cat);

$categories = array();
for(
$n=0;$n<$cat_count;$n++){
    
$categories[$n] = array('text'=>$cat["$n attr"]['text'], 'index'=>$n);
}

usort($categories'blogroll_cmp');

function 
blogroll_cmp(&$a,&$b){
    return 
strnatcasecmp($a['text'],$b['text']);
}
?>

You get the idea. Then I iterate over $categories, not $cat, but index into $cat, and do basically the exact same thing for the feeds within a category, printing out HTML along the way, etc.

Anyway, I just wanted to relay the technique of sorting indices, rather than sorting the actual data, because it comes in handy in a lot of places. If anything, sometimes it can simply be more efficient because you don't have to actually move a lot of data around within the system.

← Miller on LenoSimon builds an RSS aggregator →

Comments XML gif


Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

November 2008
SunMonTueWedThuFriSat
 1
2345678
9101112131415
16171819202122
23242526272829
30 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 5 posts

Recent comments XML

new⇒Calif. Supreme Court to take up gay marriage ban

I would argue the point is not​definitional.  While the word​marriage is su...

Justin: Nov 20, 4:37pm

Java join function

Meh, don't have null strings in​your string arrays imo, but you're​welcome ...

Keith: Nov 19, 7:51pm

Girls, please don't get breast implants

sorry but another thing i have to​make a comment on about you​men...the men...

happynow: Nov 17, 11:36pm

Books by Vincent Cheung

to all Cheung​fans:

read:

http://www.progin​osko.com/aquascum/cheung.h...

Zamir: Nov 16, 9:07am

Spider solitaire

To undo or not to undo that is the​question.
I'm an undoer. 
My dad​was n...

Can Turk: Nov 15, 2:50pm

Generated in about 0.217s.

(Used 8 db queries)

mobile phone