Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.List;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

public class Main {

    public static <T> List<T> sortCollection(List<T> collection, String sortCol, boolean isAsc) {
        for (int i = 0; i < collection.size(); i++) {
            for (int j = i + 1; j < collection.size(); j++) {
                BeanWrapper bwi = new BeanWrapperImpl(collection.get(i));
                BeanWrapper bwj = new BeanWrapperImpl(collection.get(j));
                int leftI = (Integer) bwi.getPropertyValue(sortCol);
                int leftJ = (Integer) bwj.getPropertyValue(sortCol);
                if (isAsc) {
                    if (leftI > leftJ) {
                        T obj = collection.get(j);
                        collection.set(j, collection.get(i));
                        collection.set(i, obj);
                    }
                } else {
                    if (leftI < leftJ) {
                        T obj = collection.get(j);
                        collection.set(j, collection.get(i));
                        collection.set(i, obj);
                    }
                }
            }
        }
        return collection;
    }
}