/    Sign up×
Community /Pin to ProfileBookmark

sorting an arraylist

I have an ArrayList that is populated with an object that has fields in it. I need to sort the data in this ArrayList by a piece of the data that is in my object. Is this possible? If so, how would I do it? Below is my code:

Bean code while loop that creates arraylist:

[CODE]
while (journalArticlesIterator.hasNext()) {
JournalArticle journalArticle = journalArticlesIterator.next();
String articleType = journalArticle.getType();
if (articleType.equalsIgnoreCase(“save-a-lot-slider”)) {
String articleId = journalArticle.getArticleId();
boolean articleApproved = journalArticle.getApproved();
if (articleApproved) {
JournalArticle article = JournalArticleLocalServiceUtil.getArticle(Long.parseLong(saveALotGroupId), articleId);
String image = JournalContentUtil.getValue(article, “image”);
String imageAltText = JournalContentUtil.getValue(article, “image-alt-text”);
String imageLink = JournalContentUtil.getValue(article, “image-link”);
String imageLinkWindow = JournalContentUtil.getValue(article, “image-link-window”);
String order = JournalContentUtil.getValue(article, “order”);

banner = new SaveALotHomepageBanners();
banner.setImage(image);
banner.setImageAltText(imageAltText);
banner.setImageLink(imageLink);
banner.setImageLinkWindow(imageLinkWindow);
banner.setOrder(order);
bannerList.add(banner);
}
}
}
[/CODE]

Here is the SaveALotHomepageBanners class:

[CODE]
public class SaveALotHomepageBanners {
String image = null;
String imageAltText = null;
String imageLink = null;
String imageLinkWindow = null;
String order = null;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getImageAltText() {
return imageAltText;
}
public void setImageAltText(String imageAltText) {
this.imageAltText = imageAltText;
}
public String getImageLink() {
return imageLink;
}
public void setImageLink(String imageLink) {
this.imageLink = imageLink;
}
public String getImageLinkWindow() {
return imageLinkWindow;
}
public void setImageLinkWindow(String imageLinkWindow) {
this.imageLinkWindow = imageLinkWindow;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}

}
[/CODE]

I need to sort that items in the arraylist based on the “order” field in the SaveALotHomepageBanners object.

Thanks!

to post a comment
Java

3 Comments(s)

Copy linkTweet thisAlerts:
@sohguanhOct 01.2010 — http://download.oracle.com/javase/1.5.0/docs/api/

Collections.sort(List<T> list); //for default sorting

Collections.sort(List<T> list, Comparator<? super T> c); //for customized sorting, you need to write a class to implement Comparator interface, the inside code tell Java how you want <T> to be sort
Copy linkTweet thisAlerts:
@jrthor2authorOct 01.2010 — Could someone please post some code to help me out with this?

Thanks a ton!!
Copy linkTweet thisAlerts:
@sohguanhOct 01.2010 — <i>
</i>Collections.sort(bannerList, new Comparator&lt;SaveALotHomepageBanners&gt;() {
public int compare(SaveALotHomepageBanners o1, SaveALotHomepageBanners o2) {
return (o1.getOrder()).compareTo(o2.getOrder());
}
});
×

Success!

Help @jrthor2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...