sorting two times based on status and title java android -
well have class named data:
public class data implements comparable<data>{ public string title; public string description; public string inststatus; public int imageid; data(string title, string description, int imageid,string inststatus) { this.title = title; this.description = description; this.imageid = imageid; this.inststatus = inststatus; } @override public int compareto(data another) { if(this.inststatus.equals("present")){ return this.title.compareto(another.title); }else // not present return this.inststatus.compareto(another.inststatus); // ascending } }
now if see class comparing based on title in ascending order want sorting on 2 rules
inststatus has 2 values :- "present" , "not present" 1. inststatus = "present" should come on top , after that, of them should sort in ascending order 2. inststatus = "not present" should come on bottom , after that, of them should sort in ascending order
now let's take example have list of values
title = abc ,status = present title = bcd , status not present title = xyz ,status = present title = aac ,status not present
now after above 2 rules, should after sorting
1. abc // present , ascending 2. xyz 3. aac // left ones not present , ascending 4. bcd
so, inststatus more important title. right? if then, first should check inststatus , if inststatus same after should check titles. if inststatus not same , if object present means other 1 not-present can return 1. rest same. if not present return -1.
you can implement following.
public int compareto(data another){ if(inststatus.equals(another.inststatus)){ return title.compareto(another.title); } return inststatus == "present" ? 1: -1; }
Comments
Post a Comment