3) controller

@RequestMapping(value = "/courseSortUpdate",method = RequestMethod.GET,produces = {"text/html;charset=UTF-8"})
    public String courseSortUpdate(HttpServletRequest request){
        String sort=request.getParameter("sort");
        String sort1=sort.substring(0,6);
        WExcellentCourseEntity entity=courseService.infoBySort(sort1);
        if(sort.substring(sort.length()-1).equals("1")){
            WExcellentCourseEntity entity1=courseService.sortUp(sort1);           courseService.updateBySort(entity1.getSort(),entity.getId());
            courseService.updateBySort(Integer.parseInt(sort1),entity1.getId());
        }
        if(sort.substring(sort.length()-1).equals("2")){
            WExcellentCourseEntity entity2=courseService.sortDown(sort1);
            courseService.updateBySort(entity2.getSort(),entity.getId());
            courseService.updateBySort(Integer.parseInt(sort1),entity2.getId());
        }
        return "operation/ecList";
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

二、列表中某行的置顶操作 
1、思路依然是sort值的交换,只是除了普通的交换之外,还需要list的Collections.swap()方法。 
2、如下是一个简单的demo:

public class stringTest {

    public static void main (String[] args){
        List<userTest> list=new ArrayList<>();
        userTest u1=new userTest();
        u1.setId(1);
        u1.setName("a");
        u1.setSort(1);
        userTest u2=new userTest();
        u2.setId(2);
        u2.setName("b");
        u2.setSort(2);
        userTest u3=new userTest();
        u3.setId(3);
        u3.setName("c");
        u3.setSort(3);
        userTest u4=new userTest();
        u4.setId(4);
        u4.setName("d");
        u4.setSort(4);
        userTest u5=new userTest();
        u5.setId(5);
        u5.setName("e");
        u5.setSort(5);
        userTest u6=new userTest();
        u6.setId(6);
        u6.setName("f");
        u6.setSort(6);
        userTest u7=new userTest();
        u7.setId(7);
        u7.setName("g");
        u7.setSort(7);

        list.add(u1);
        list.add(u2);
        list.add(u3);
        list.add(u4);
        list.add(u5);
        list.add(u6);
        list.add(u7);


        //1、交换list的0号数据和6号数据
        swap2(list, 6, 0);
        for (userTest e : list) {
            System.out.println(e.getId()+" "+e.getName() + " "+e.getSort()+" ");
        }
        System.out.println("-------------");
        //2、依次交换sort数据
        for(int i=0;i<list.size()-1;i++){
            int sort=list.get(i+1).getSort();
            list.get(i+1).setSort(list.get(i).getSort());
            list.get(i).setSort(sort);
        }
        for (userTest e : list) {
            System.out.println(e.getId()+" "+e.getName() + " "+e.getSort()+" ");
        }
    }

    public static <T> void swap2(List<T> list, int oldPosition, int newPosition) {
        if (null == list) {
            throw new IllegalStateException("The list can not be empty...");
        }
        if (oldPosition > newPosition) {
            for (int i = oldPosition; i > newPosition; i--) {
                Collections.swap(list, i, i - 1);
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

运行截图如下: 
java实现列表的上下移动和置顶操作_Java 
可以看到list已经按照我们所想排列。 
这两个功能实现只是我个人浅薄的认识,若有更好的解决办法还望各位大神指教。