KBD

Keith Devens .com

Saturday, July 4, 2009 Flag waving
Nonsense on stilts! – Derived from Jeremy Bentham
← 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)
:

July 2009
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



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

Recent comments XML

new⇒Wizard's First Rule

> while it is cheesy to some​extent, I actually found it to be​pretty enjoy...

Keith: Jul 3, 6:33pm

I hate Norton Antivirus

I bought Norton 2009 and it is not​installing on my computer!!!
It​seems l...

o'neil: Jun 30, 11:44am

Generated in about 0.161s.

(Used 8 db queries)