Java komparatoridan foydalanib ob'ektni tartiblash, lekin belgilangan qiymat bilan tartiblangan ro'yxatda oxirgi qiymat bo'lishi kerak

Menda alifbo tartibida bo'lgan toifadagi toifadagi toifadagi kodga asosan MasterPayee ob'ektini saralashim bor, endi boshqa xizmatlar kategoriya kodini tartiblangan ro'yxatda bo'lish uchun olishim kerak

Tartiblashdan keyin ro'yxat

Financial and Insurance services

Government Sectors

Other Services

Telecommunications and Utilities

Transportation Services

kerakli ro'yxat quyidagi tarzda

Financial and Insurance services
Government Sectors
Telecommunications and Utilities
Transportation Services
Other Services

Ro'yxatning oxirgi qismida boshqa xizmatlarni yoqishingiz kerak Quyidagi taqqoslash qiluvchi ro'yxatni tartiblashtirish uchun foydalanadi

Collections.sort(masterPayees, getCategoryNameComparatorByMasterPayee());

private Comparator getCategoryNameComparatorByMasterPayee() {
    Comparator categoryNameComparatorByMasterPayee = new Comparator() {
        public int compare(MasterPayee o1, MasterPayee o2) {
            return (((MasterPayee) o1).getPayee_category().toString()
                    .compareToIgnoreCase(((MasterPayee) o2).getPayee_category().toString()));
        }

    };
    return categoryNameComparatorByMasterPayee;
}

Other Services should be always last in the sorted list

2

7 javoblar

Buni ko'ring:

Comparator categoryNameComparatorByMasterPayee = new Comparator(){
    public int compare(MasterPayee o1, MasterPayee o2) {
        if (((MasterPayee) o1).getPayee_category().toString().equalsIgnoreCase("Other Services") && ((MasterPayee) o1).getPayee_category().toString().equalsIgnoreCase(((MasterPayee) o2).getPayee_category().toString())) {
            return 0;
        }
        else if (((MasterPayee) o1).getPayee_category().toString().equalsIgnoreCase("Other Services")) {
            return 1;
        }
        else if (((MasterPayee) o2).getPayee_category().toString().equalsIgnoreCase("Other Services")) {
            return -1;
        }
        else return (((MasterPayee) o1).getPayee_category().toString().compareToIgnoreCase(((MasterPayee) o2).getPayee_category().toString()));
    }
};

"Boshqa xizmatlar" bilan ob'ektni har doim "katta" deb hisoblaydi, natijada u oxirida paydo bo'ladi.

3
qo'shib qo'ydi
Kutilganidek ishlaydi
qo'shib qo'ydi muallif Kiran, manba

Boshqa takliflar "Boshqa xizmatlar" har doim mavjud bo'lsa, uni ro'yxatdan olib tashlash, tartiblashni amalga oshirish va keyin "Boshqa xizmatlar" ni qo'shish. Shunday qilib siz tartiblash mantiqini soddalashtirasiz va istisnoni alohida-alohida qo'shishingiz mumkin.

Agar har doim mavjud bo'lmasa, unda siz avval uni qidirib topsangiz, u holda uni qo'shib qo'ying.

0
qo'shib qo'ydi

Menimcha, uch tomonlama ifodani qo'llash orqali mantiqni to'g'ri bajarishimiz mumkin.

private Comparator getCategoryNameComparatorByMasterPayee() {
    Comparator categoryNameComparatorByMasterPayee = new Comparator() {
        public int compare(MasterPayee o1, MasterPayee o2) {
            String s1 = ((MasterPayee) o1).getPayee_category().toString();
            String s2 = ((MasterPayee) o1).getPayee_category().toString();
            boolean b1 = s1.equalsIgnoreCase("Other Services");
            boolean b2 = s2.equalsIgnoreCase("Other Services");

            return b1 ? (b2 ? 0 : 1) : (b2 ? -1 : s1.compareToIgnoreCase(s2));
        }
    };
    return categoryNameComparatorByMasterPayee;
}

Bu kodni o'qishni qiyinlashtirmaydi, shuning uchun uni saqlash qiyin. Agar bu erda mantiqni o'zgartirish kerak bo'lsa, biz faqat minimal o'zgarishlar qilishimiz kerak.

0
qo'shib qo'ydi
@Kiran Kechirasiz, men String # equalsIgnoreCase() dan foydalanishni maqsad qilgan edim.
qo'shib qo'ydi muallif Tim Biegeleisen, manba
Bu erda b1 va b2 int ga qaytadi va kompilyatsiya vaqtining xatosini chiqaradi
qo'shib qo'ydi muallif Kiran, manba

Agar ushbu elementlarning cheklangan miqdori mavjud bo'lsa, ularni enum deb yozib qo'yaman.

Chiqish matni uchun nomi va tartibida tartiblash uchun tartibidagi . Bu toza.

0
qo'shib qo'ydi

Create a constant map and in the comparator use the value.

0
qo'shib qo'ydi

Agar tartiblangan bo'lishi mumkin bo'lgan barcha qiymatlarni bilsangiz, siz guava'a buyurtma berishingiz mumkin.

Comparatorni yaratish uchun quyidagi kabi qiymatlarni belgilashingiz mumkin:

Ordering ordering1 =  Ordering.explicit("Financial and Insurance services","Government Sectors","Telecommunications and Utilities","Transportation Services","Other Services");

Siz shuningdek, Ordering.explicit() parametrlari uchun o'zingizning qadriyatlaringiz bilan Listni taqdim etishingiz mumkin.

0
qo'shib qo'ydi
Siz haqsiz. Bajarildi!
qo'shib qo'ydi muallif MicD, manba
Ehtimol, bu javobga bir oz batafsil ma'lumot kiritishingiz kerak.
qo'shib qo'ydi muallif Lupus Nox, manba

Agar strings ro'yxati ro'xsat etilgan buyurtma mag'lubiyatga emas, balki ish mantiqiga asoslangan bo'lsa, keyin EnumMap koleksiyonlaridan foydalanishni tavsiya etaman.

enum Industry{
    FINANCE, GOVERNMENT, UTILITIES, TRANSPORT, OTHER
}

public class StreamEnumMap {
    public static void main(String... strings){
        Map industryMap = new EnumMap<>(Industry.class);
        industryMap.put(Industry.FINANCE, "Financial and Insurance services");
        industryMap.put(Industry.GOVERNMENT,"Government Sectors");
        industryMap.put(Industry.UTILITIES,"Telecommunications and Utilities");
        industryMap.put(Industry.OTHER,"Other Services");
        industryMap.put(Industry.TRANSPORT, "Transportation Services");

        industryMap.values().stream().forEach(System.out::println);
    }
}

Bu natijalarni quyidagi tartibda ishlab chiqaradi,

Financial and Insurance services
Government Sectors
Telecommunications and Utilities
Transportation Services
Other Services
0
qo'shib qo'ydi
@kiran Jadvalda ustuvor ustunni kiritish mumkin bo'lsa, unda tartiblashtirish uchun ikkala ustunni ham foydalanishingiz mumkin. Shaxsan men string tartibida solishtirish bilan raqamli tartiblashni afzal ko'rardim. Bundan tashqari, ustun ustunni qo'llash mantiqni JB-ga o'zgartiradi. Kelajakda buyurtmani o'zgartirish kerak bo'lsa, bu osonroq bo'ladi
qo'shib qo'ydi muallif Sekar, manba
Idea yaxshi, lekin bu qadriyatlarni DB dan qabul qilaman. Men kelajakda qancha qo'shilganini bilmayman
qo'shib qo'ydi muallif Kiran, manba