View Full Version : custom sorting of rss feeds - gajaxrssticker
yuengling25
12-03-2013, 04:20 PM
1) Script Title: gajaxrssticker
2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex18/gajaxrssticker.htm
3) Describe problem: Not really a problem. More of a presentation question. I'm currently using this script (with gfeedfetcher) in a sports news/live scores ticker implementation on both my personal website and for a mobile widget for the iPhone. My question has to do with the instance.filterfeed(int, ["sortby"]) method, particularly the "sortby" parameter. Is there a way to set a custom sort on the label, so that the feeds are displayed in an order that I specify rather than alphabetically?
Thanks in advance for your help!
jscheuer1
12-03-2013, 05:13 PM
Probably not without modifying the script. But how do you want things sorted? If you do not specify any sorting method:
instance.filterfeed(int)
it will sort by date, showing the most current first.
If you want to introduce your own sorting method, this function in gfeedfetcher.js can be modified:
gfeedfetcher._sortarray=function(arr, sortstr){
var sortstr=(sortstr=="label")? "ddlabel" : sortstr //change "label" string (if entered) to "ddlabel" instead, for internal use
if (sortstr=="title" || sortstr=="ddlabel"){ //sort array by "title" or "ddlabel" property of RSS feed entries[]
arr.sort(function(a,b){
var fielda=a[sortstr].toLowerCase()
var fieldb=b[sortstr].toLowerCase()
return (fielda<fieldb)? -1 : (fielda>fieldb)? 1 : 0
})
}
else{ //else, sort by "publishedDate" property (using error handling, as "publishedDate" may not be a valid date str if an error has occured while getting feed
try{
arr.sort(function(a,b){return new Date(b.publishedDate)-new Date(a.publishedDate)})
}
catch(err){}
}
}
If you think you need to modify it but don't know how, tell me how you want things sorted.
yuengling25
12-03-2013, 06:20 PM
I'm definitely going to need to modify the script.
I have two rss feeds for sports news headlines (labels: "ESPN" and "SkySports"), one feed each for scores in pro baseball ("MLB"), pro basketball ("NBA"), pro football ("NFL"), pro hockey ("NHL"), golf ("GOLF"), college football ("NCF") and college basketball ("NCB"). And then seven feeds for professional soccer (labels: "EPL", "LFP", "GER", "SERIE A", "UCL", "UEL", "MLS").
So that's a total of 16 feeds. I want to sort them by label so they report in the following order:
1. ESPN
2. SkySports
3. NFL
4. MLB
5. NHL
6. NBA
7. NCF
8. NCB
9. EPL
10. LFP
11. GER
12. SERIE A
13. UCL
14. UEL
15. MLS
16. GOLF
Like I said, I think this is definitely a code modification job, which I have no idea to do in js. I usually hate just outright asking people to "just do this for me", but would greatly appreciate your help on this John.
jscheuer1
12-03-2013, 07:40 PM
OK, we may have to play around with this once we set it up (fix any errors, maybe make tweaks). But let's start out with this. Using a text only editor like NotePad, change the gfeedfetcher._sortarray function in the gfeedfetcher.js file to this (addition highlighted):
gfeedfetcher._sortarray=function(arr, sortstr){
var sortstr=(sortstr=="label")? "ddlabel" : sortstr //change "label" string (if entered) to "ddlabel" instead, for internal use
if (sortstr=="title" || sortstr=="ddlabel"){ //sort array by "title" or "ddlabel" property of RSS feed entries[]
arr.sort(function(a,b){
var fielda=a[sortstr].toLowerCase()
var fieldb=b[sortstr].toLowerCase()
return (fielda<fieldb)? -1 : (fielda>fieldb)? 1 : 0
})
}
else{ //else, sort by "publishedDate" property (using error handling, as "publishedDate" may not be a valid date str if an error has occured while getting feed
try{
arr.sort(function(a,b){return new Date(b.publishedDate)-new Date(a.publishedDate)})
}
catch(err){}
}
if(sortstr.indexOf('customlabel:') == 0){
sortstr = ',' + sortstr.substring(12) + ',';
arr.sort(function(a, b){
return sortstr.indexOf(',' + a.ddlabel + ',') - sortstr.indexOf(',' + b.ddlabel + ',');
});
}
}
Save and use that version of the script.
Now, where you do this when setting the filter for the feed:
instance.filterfeed(int, ["sortby"])
You can use a string like the one shown:
instance.filterfeed(int, 'customlabel:ESPN,SkySports,NFL,MLB,NHL,NBA,NCF,NCB,EPL,LFP,GER,SERIE A,UCL,UEL,MLS,GOLF')
It should sort the feeds the way you want them. Of course you must change instance to the actual instance and change int to an unquoted number representing the maximum number of items to display.
The browser cache may need to be cleared and/or the page refreshed to see changes.
If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.
yuengling25
12-03-2013, 09:01 PM
OK, so my first stab at implementing your suggested modifications (and making sure and then double sure that I did things to a tee), it appears that the feeds are being displayed according to the default of "date"... almost as if it's ignoring the changes that I made. Any thoughts? I'm not able to give you a link to the page right now, as I am testing things with my phone widget implementation. Going to tinker with it further when I get home later this evening.
jscheuer1
12-04-2013, 02:15 AM
I see an error I made, the script uses ddlabel, not label. So the changed function should be:
gfeedfetcher._sortarray=function(arr, sortstr){
var sortstr=(sortstr=="label")? "ddlabel" : sortstr //change "label" string (if entered) to "ddlabel" instead, for internal use
if (sortstr=="title" || sortstr=="ddlabel"){ //sort array by "title" or "ddlabel" property of RSS feed entries[]
arr.sort(function(a,b){
var fielda=a[sortstr].toLowerCase()
var fieldb=b[sortstr].toLowerCase()
return (fielda<fieldb)? -1 : (fielda>fieldb)? 1 : 0
})
}
else{ //else, sort by "publishedDate" property (using error handling, as "publishedDate" may not be a valid date str if an error has occured while getting feed
try{
arr.sort(function(a,b){return new Date(b.publishedDate)-new Date(a.publishedDate)})
}
catch(err){}
}
if(sortstr.indexOf('customlabel:') == 0){
sortstr = ',' + sortstr.substring(12) + ',';
arr.sort(function(a, b){
return sortstr.indexOf(',' + a.ddlabel + ',') - sortstr.indexOf(',' + b.ddlabel + ',');
});
}
}
yuengling25
12-04-2013, 12:57 PM
Works a charm now after applying your revision! Thanks John, always a big help!
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.