We help IT Professionals succeed at work.

Sort an rss feed in a CEWP

How can I sort this rss feed alphabetically?

This is how I'm displaying it now:

<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAzOfx2c8w9UU2piCi-QfziRRwcONvftZBEURdgjnJbq9oC0AJ3BRlDevnMejQ1U5ax6a0JLTNKz42rw"></script>
<style text="text/css">
#positions-wrapper {
font-family: Arial, Helvetica;
font-size: 11px;
}
#positions {
border: 1px solid #eee;
padding: 0px 5px 5px 5px;
margin-top: 5p;x
}
#positions-header {
padding: 0px 5px 5px 5px;
font-weight: bold;
}
#positions a {
color: #4C4C4C;
text-decoration: none;
padding-bottom: 0px;
border-bottom: 1px dotted #888;
}
#positions a:hover {
border-bottom: 1px solid #888;
}
#positions .position-odd {
background-color: #e5e1dc;
}
#positions div {
padding: 5px;
}
</style>

<script type="text/javascript">
google.load("feeds", "1");
google.setOnLoadCallback(initialize);

function initialize() {
	var feed = new google.feeds.Feed('http://test.testdomain.com/rss_feed_clean.aspx');
	feed.setNumEntries(100);
	feed.load(function(result) {
		if (!result.error) {
			var container = document.getElementById("feed");
			
			for (var i = 0; i < result.feed.entries.length; i++) {
				var entry = result.feed.entries[i];
				var title = entry.title;
				var link = entry.link;
				
				jQuery("#positions").append("<div><a href=\"" + link + "\" target=\"_blank\">" + title + "</a></div>");
			}
			
			jQuery("#positions div:odd").addClass("position-odd");
		}
	});
}
</script>

<div id="positions-wrapper">
	<div id="positions">
<div id="positions-header"><a href="http://test.testdomain.com/" target="_blank">View Open Position List</a></div>

</div>
</div>

Open in new window

Comment
Watch Question

Ivan PadabedDirector, Technology Solutions

Commented:
I would go for custom JS comparer based on Title, then pass it as argument to result.feed.entries.sort(myComparer) befor iterating through it

something like that:
result.feed.entries.sort(function(a, b){
 var titleA=a.Title.toLowerCase(), titleB=b.Title.toLowerCase()
 if (titleA< titleB) //sort string ascending
  return -1 
 if (titleA> titleB)
  return 1
 return 0 //default return value (no sorting)
})

Open in new window

Author

Commented:
thanks! But where would this go in current code?
Ivan PadabedDirector, Technology Solutions

Commented:
I would try that way:
<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAzOfx2c8w9UU2piCi-QfziRRwcONvftZBEURdgjnJbq9oC0AJ3BRlDevnMejQ1U5ax6a0JLTNKz42rw"></script>
<style text="text/css">
#positions-wrapper {
font-family: Arial, Helvetica;
font-size: 11px;
}
#positions {
border: 1px solid #eee;
padding: 0px 5px 5px 5px;
margin-top: 5p;x
}
#positions-header {
padding: 0px 5px 5px 5px;
font-weight: bold;
}
#positions a {
color: #4C4C4C;
text-decoration: none;
padding-bottom: 0px;
border-bottom: 1px dotted #888;
}
#positions a:hover {
border-bottom: 1px solid #888;
}
#positions .position-odd {
background-color: #e5e1dc;
}
#positions div {
padding: 5px;
}
</style>

<script type="text/javascript">
google.load("feeds", "1");
google.setOnLoadCallback(initialize);

function initialize() {
	var feed = new google.feeds.Feed('http://test.testdomain.com/rss_feed_clean.aspx');
	feed.setNumEntries(100);
	feed.load(function(result) {
		if (!result.error) {
			var container = document.getElementById("feed");
			//*** adding sort by title ***
result.feed.entries.sort(function(a, b){
 var titleA=a.Title.toLowerCase();
 titleB=b.Title.toLowerCase();
 if (titleA< titleB)
  return -1 
 if (titleA> titleB)
  return 1
 return 0
})
                        //*** process sorted array
			for (var i = 0; i < result.feed.entries.length; i++) {
				var entry = result.feed.entries[i];
				var title = entry.title;
				var link = entry.link;
				
				jQuery("#positions").append("<div><a href=\"" + link + "\" target=\"_blank\">" + title + "</a></div>");
			}
			
			jQuery("#positions div:odd").addClass("position-odd");
		}
	});
}
</script>

<div id="positions-wrapper">
	<div id="positions">
<div id="positions-header"><a href="http://test.testdomain.com/" target="_blank">View Open Position List</a></div>

</div>
</div>

Open in new window


please let me know the result, unfortunately I can't test it on my environment so there could be issues

Author

Commented:
It's not bringing back any results at all.

can I setup an 'alert' popup or something? how can I test?
Ivan PadabedDirector, Technology Solutions

Commented:
you can try to set up debugger and go step by step through the code: update the sorting method as listed below:

result.feed.entries.sort(function(a, b){
debugger;
 var titleA=a.Title.toLowerCase();
 titleB=b.Title.toLowerCase();
 if (titleA< titleB)
  return -1 
 if (titleA> titleB)
  return 1
 return 0
})

Open in new window

Author

Commented:
i get this error in the debugger:

SCRIPT5007: Unable to get value of the property 'toLowerCase': object is null or undefined

Author

Commented:
more detail:


Line 13: titleB=b.Title.toLowerCase();

'titleB' is not defined.

 

Line 15: return -1

Expected '{' and instead saw 'return'.
Ivan PadabedDirector, Technology Solutions

Commented:
looks like I used a wrong case on entries property. please try this:

result.feed.entries.sort(function(a, b){
 var titleA=a.title.toLowerCase();
 var titleB=b.title.toLowerCase();
 if (titleA< titleB)
  return -1 
 if (titleA> titleB)
  return 1
 return 0
})

Open in new window

Director, Technology Solutions
Commented:
more adjustments to try:

result.feed.entries.sort(function(a, b){
 var titleA=a.title.toLowerCase();
 var titleB=b.title.toLowerCase();
 if (titleA< titleB)
  {return -1 ;}
 if (titleA> titleB)
  {return 1;}
 return 0
})

Open in new window

Author

Commented:
awesome - that did the trick. muchas thanks