sort 관련(vo)

2014. 3. 19. 23:22Language/Java

반응형



이번 포스팅은 sort 관련 입니다. 

특히, vo sortting 하는 것을 한번 알아 보겠습니다.


우선 테스트로 vo를 만들어 List에 담고나서 제대로 담겼는지 size를 한번 재보도록 하겠습니다.


[간단 테스트 소스 작성 시작!]           

       @Test

       public void insertVo(){

testList = new ArrayList<TestVo>();

System.out.println("List Size(1) :"+testList.size());

for(int i=0; i < 2; i++){

testVo = new TestVo();

if(i == 0){

  testVo.setIndexKey("DSP001"); 

  testVo.setSortStandard("330");

}else{

testVo.setIndexKey("DSP002");

testVo.setSortStandard("120");

}

testList.add(testVo);

}

System.out.println("List Size(2) :"+testList.size());

} 


결과

 List Size(1) :0 

 List Size(2) :2


 2개의 데이터가 들어가 있군요^-^good~의심이 되신다면! Debug mode로 확인 해 보세용~ 



SORT를 이제 해볼까용?


[전체 소스]

package kr.pe.acet.voSort;

import static org.junit.Assert.*;


import java.util.ArrayList;

import java.util.List;


import org.junit.Before;

import org.junit.Test;


public class VoSortTest {

private TestVo testVo;

List<TestVo> testList;


@Before

public void insertVo(){

testList = new ArrayList<TestVo>();

System.out.println("List Size(1) :"+testList.size());

for(int i=0; i < 2; i++){

testVo = new TestVo();

if(i == 0){

  testVo.setIndexKey("DSP001");

  testVo.setSortStandard("330");

  testVo.setItem("snack");

}else{

testVo.setIndexKey("DSP002");

testVo.setSortStandard("120");

testVo.setItem("ice-cream");

}

testList.add(testVo);

}

System.out.println("List Size(2) :"+testList.size());

}

@Test

public void voSortTest() {

  // List > Vo > String   

  List<String> sortTargetList = new ArrayList<String>();

  for(int l=0; l < testList.size(); l++){

  sortTargetList.add(testList.get(l).getSortStandard());

  }

       Object[] notSortedArray = sortTargetList.toArray();

       java.util.Arrays.sort(notSortedArray);

      // System.out.println(java.util.Arrays.asList(notSortedArray));

       // [120, 330]

       

       for(int t=0; t < 2; t++){

     System.out.println(notSortedArray[t]);

          for(int t2=0; t2 < testList.size(); t2++){

         if(testList.get(t2).getSortStandard().equals(notSortedArray[t])){ 

                 // 이미 정렬되어진 배열에서 하나 꺼내와서 for문을 돌면서 vo들과 비교를 합니다.

                 // 비교를 하면서 OK!이면 아래처럼 정보를 빼내 옵니다.

         System.out.println(testList.get(t2).getIndexKey()+"|"+testList.get(t2).getItem()+"|"+testList.get(t2).getSortStandard());

         /*

          * private String indexKey;

private String sortStandard;

private String item; 

          * 

          */

         

         }

          }

       }


}


}

 



[결과]

List Size(1) :0

List Size(2) :2

120

DSP002|ice-cream|120

330

DSP001|snack|330 



[참고 소스]

    Array.class

      /**     

     * Sorts the specified array of objects into ascending order, according to

     * the {@linkplain Comparable natural ordering}

     * of its elements.  All elements in the array

     * must implement the {@link Comparable} interface.  Furthermore, all

     * elements in the array must be <i>mutually comparable</i> (that is,

     * <tt>e1.compareTo(e2)</tt> must not throw a <tt>ClassCastException</tt>

     * for any elements <tt>e1</tt> and <tt>e2</tt> in the array).<p>

     *

     * This sort is guaranteed to be <i>stable</i>:  equal elements will

     * not be reordered as a result of the sort.<p>

     *

     * The sorting algorithm is a modified mergesort (in which the merge is

     * omitted if the highest element in the low sublist is less than the

     * lowest element in the high sublist).  This algorithm offers guaranteed

     * n*log(n) performance.

     *

     * @param a the array to be sorted

     * @throws  ClassCastException if the array contains elements that are not

     * <i>mutually comparable</i> (for example, strings and integers).

     */

    public static void sort(Object[] a) {

        Object[] aux = (Object[])a.clone();

        mergeSort(aux, a, 0, a.length, 0);

    } 


um..위와 같이 하면 끝일까요??


아닙니다~만약에~만약에 120 330 330 330 이런식으로 있다면? 맞습니다. sortting의 기준이 중복이 된다면

그다음에는 무엇으로 정렬을 해야하는지도 정해줘야 할 것 같습니다.


보통 쇼핑몰 같은 곳에서는 검색엔진을 사용하는데.. 미리 수집되어진 SCD라는 것이 있습니다.

요녀석들의 각각의 태그에 가중치를 부여 할 수 있습니다.  그래서 가중치로 순서가 정해 집니다. 

즉, sort+가중치겠죠..^^;

뭐..검색엔진 마다 다를 수도 있겠지만..제 경험상은 그러합니다.


즉, 이중 정렬도 고려해볼 필요가 있을 것 같습니다.

그리고 정렬을 120 330 이 아닌 330 120 이런 순서로 하려면???  

아시는 분은 댓글을 통한 소통을 부탁드립니다 (- - * (__* ~꾸벅

그럼 이만..ㅌㅌㅌ


                           - END -



반응형