Results 1 to 7 of 7

Thread: custom sorting of rss feeds - gajaxrssticker

  1. #1
    Join Date
    Aug 2013
    Location
    PGH PA US
    Posts
    12
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default custom sorting of rss feeds - gajaxrssticker

    1) Script Title: gajaxrssticker

    2) Script URL (on DD): http://www.dynamicdrive.com/dynamici...xrssticker.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!

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,070
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    Probably not without modifying the script. But how do you want things sorted? If you do not specify any sorting method:

    Code:
    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:

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    yuengling25 (12-04-2013)

  4. #3
    Join Date
    Aug 2013
    Location
    PGH PA US
    Posts
    12
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    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.

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,070
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    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):

    Code:
    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:

    Code:
    instance.filterfeed(int, ["sortby"])
    You can use a string like the one shown:

    Code:
    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.
    Last edited by jscheuer1; 12-04-2013 at 05:18 PM. Reason: fix code
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. The Following User Says Thank You to jscheuer1 For This Useful Post:

    yuengling25 (12-04-2013)

  7. #5
    Join Date
    Aug 2013
    Location
    PGH PA US
    Posts
    12
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    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.

  8. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,070
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    I see an error I made, the script uses ddlabel, not label. So the changed function should be:

    Code:
    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 + ',');
    		});
    	}
    }
    Last edited by jscheuer1; 12-04-2013 at 05:19 PM. Reason: code improvement
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. The Following User Says Thank You to jscheuer1 For This Useful Post:

    yuengling25 (12-04-2013)

  10. #7
    Join Date
    Aug 2013
    Location
    PGH PA US
    Posts
    12
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Works a charm now after applying your revision! Thanks John, always a big help!

Similar Threads

  1. Incorporating fade in/out of rss items - gajaxrssticker
    By yuengling25 in forum Dynamic Drive scripts help
    Replies: 10
    Last Post: 09-19-2013, 04:24 AM
  2. gAjax RSS Feeds Displayer - Edit Feeds Shown
    By dsol828 in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 08-01-2012, 07:11 PM
  3. gAjax RSS Feeds Displayer - ignore broken feeds?
    By kblm in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 05-24-2012, 10:18 PM
  4. Replies: 0
    Last Post: 09-05-2011, 09:18 AM
  5. gAjax RSS Feeds Displayer - can't display multiple feeds
    By nsbrown in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 06-06-2008, 08:40 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •