我是根据movesort来排序的,如果我的movesort是13579的话 就排序不了了

解决方案 »

  1.   


    需求就是为什么1234的话我可以随意上下移动,。 但是13579就不行,移动不了 @Override
    public int updateSort(String notepadId, String moveSort) {
    ATextNotepad notePad = this.getNotePadById(Long.valueOf(notepadId));
    //得到该记录的sort
    long sort = notePad.getMovesort().longValue();
    long sort1;
    if("0" == moveSort || moveSort.equals("0")){//向上
    sort1 = sort - 1;
    }else{
    sort1 = sort + 1;
    }
    List list = this.notepaddao.getNotePadList("from ATextNotepad t where t.movesort="+sort1) ;
    if(list.size()>0){
    ATextNotepad notePad1 = (ATextNotepad) list.get(0);
    Long s1 = notePad1.getMovesort();
    Long s2 = notePad.getMovesort();
    notePad.setMovesort(s1);
    notePad1.setMovesort(s2);
    this.notepaddao.updateNotePad(notePad);
    this.notepaddao.updateNotePad(notePad1);
    }
    return 0;
    }
      

  2.   

    找前一个的算法是 movesort 小于当前movesort的第一个结果。你非要自己定义movesort -1去按 movesort 查询当然找不到
      

  3.   

    我只是来混个每日10分的,
     参考下这个下面的其他回答: https://zhidao.baidu.com/question/574533221.html
      

  4.   

    @Override
    public int updateSort(String notepadId, String moveSort) {
    ATextNotepad notePad = this.getNotePadById(Long.valueOf(notepadId));
    //得到该记录的sort
    long sort = notePad.getMovesort().longValue();
    String sql="";
    if("0" == moveSort || moveSort.equals("0")){//向上
    sql="t.movesort<sort order by t.movesort DESC";
    }else{
    sql="t.movesort>sort order by t.movesort ";
    }
    List list = this.notepaddao.getNotePadList("from ATextNotepad t where "+sql+" LIMIT 0,1") ;
    if(list.size()>0){
    ATextNotepad notePad1 = (ATextNotepad) list.get(0);
    Long s1 = notePad1.getMovesort();
    Long s2 = notePad.getMovesort();
    notePad.setMovesort(s1);
    notePad1.setMovesort(s2);
    this.notepaddao.updateNotePad(notePad);
    this.notepaddao.updateNotePad(notePad1);
    }
    return 0;
    }
      

  5.   

    if("0" == moveSort || moveSort.equals("0")){//向上
    改为
    if(moveSort != null && "0".equals(moveSort)){//向上第一: "0" == moveSort 字符串不能用 == 来判断,要用 equals 来比较;
    第二:moveSort.equals("0") 要把"0" 放到equals前面 "0".equals(moveSort) ,这样moveSort为空的时候也不会报错,如果moveSort为空会报错;