/* sortingNumber() : 숫자인 실수만으로 되어있을 때, 적용될 함수 */
function sortingNumber( a , b ){
if ( typeof a == "number" && typeof b == "number" ) return a - b;
// 천단위 쉼표와 공백문자만 삭제하기.
var a = ( a + "" ).replace( /[,\s\xA0]+/g , "" );
var b = ( b + "" ).replace( /[,\s\xA0]+/g , "" );
var numA = parseFloat( a ) + "";
var numB = parseFloat( b ) + "";
if ( numA == "NaN" || numB == "NaN" || a != numA || b != numB ) return false;
return parseFloat( a ) - parseFloat( b );
}
/* changeForSorting() : 문자열 바꾸기. */
function changeForSorting( first , second ){
// 문자열의 복사본 만들기.
var a = first.toString().replace( /[\s\xA0]+/g , " " );
var b = second.toString().replace( /[\s\xA0]+/g , " " );
var change = { first : a, second : b };
if ( a.search( /\d/ ) < 0 || b.search( /\d/ ) < 0 || a.length == 0 || b.length == 0 ) return change;
var regExp = /(\d),(\d)/g; // 천단위 쉼표를 찾기 위한 정규식.
a = a.replace( regExp , "$1" + "$2" );
b = b.replace( regExp , "$1" + "$2" );
var unit = 0;
var aNb = a + " " + b;
var numbers = aNb.match( /\d+/g ); // 문자열에 들어있는 숫자 찾기
for ( var x = 0; x < numbers.length; x++ ){
var length = numbers[ x ].length;
if ( unit < length ) unit = length;
}
var addZero = function( string ){ // 숫자들의 단위 맞추기
var match = string.match( /^0+/ );
if ( string.length == unit ) return ( match == null ) ? string : match + string;
var zero = "0";
for ( var x = string.length; x < unit; x++ ) string = zero + string;
return ( match == null ) ? string : match + string;
};
change.first = a.replace( /\d+/g, addZero );
change.second = b.replace( /\d+/g, addZero );
return change;
}
/* ordinary()
*
* 1. ascending : 오름차순
* 2. descending : 내림차순
*/
function ordinary(){
var compare = function( a , b ){
var sorting = sortingNumber( a , b );
if ( typeof sorting == "number" ) return sorting;
var change = changeForSorting( a , b );
var a = change.first;
var b = change.second;
return ( a < b ) ? -1 : ( a == b ) ? 0 : 1;
};
var ascendingOrder = function( a , b ){ return compare( a , b ); };
var descendingOrder = function( a , b ){ return compare( b , a ); };
return { ascending : ascendingOrder, descending : descendingOrder };
}
/* ignoreCase() : 대소문자 무시 ( toLowerCase )
*
* 1. ascending : 오름차순
* 2. descending : 내림차순
*/
function ignoreCase(){
var compare = function( a , b ){
var sorting = sortingNumber( a , b );
if ( typeof sorting == "number" ) return sorting;
var change = changeForSorting( a , b );
var a = change.first.toLowerCase();
var b = change.second.toLowerCase();
return ( a < b ) ? -1 : ( a == b ) ? 0 : 1; // 또는, return a.localeCompare ( b );
};
var ascendingOrder = function( a , b ){ return compare( a , b ); };
var descendingOrder = function( a , b ){ return compare( b , a ); };
return { ascending : ascendingOrder, descending : descendingOrder };
}
/* byLocale() : 로컬에 따라 ( localeCompare ) - 언어에 따라, 결과가 다를 수 있음.
*
* 1. ascending : 오름차순
* 2. descending : 내림차순
*/
function byLocale(){
var compare = function( a , b ){
var sorting = sortingNumber( a , b );
if ( typeof sorting == "number" ) return sorting;
var change = changeForSorting( a , b );
var a = change.first;
var b = change.second;
return a.localeCompare( b );
};
var ascendingOrder = function( a , b ){ return compare( a , b ); };
var descendingOrder = function( a , b ){ return compare( b , a ); };
return { ascending : ascendingOrder, descending : descendingOrder };
}
// 실제 소스
var selectObj = $('#sortOrdr');
var order = ordinary(); // js 순서정렬 공통함수
var originSortOrdr = [];
var domnSortOrdr = [];
$.each(result.domnList, function(index, item){
originSortOrdr.push(item.sortOrdr);
});
// sortOrdr 순차 정렬
domnSortOrdr = originSortOrdr.sort( order.ascending );
selectObj.empty();
$.each(domnSortOrdr, function(index, item){
var option = $("" + item + "");
if(item == treeNode.sortOrdr)
option = $("" + item + "")
selectObj.append(option);
});
'Client Standard > JavaScript & jQuery' 카테고리의 다른 글
[js] delete obj.key (0) | 2020.10.22 |
---|---|
[Javascript] date sorting (0) | 2020.06.30 |
[Tag] br 태그 줄바꿈 (0) | 2019.02.07 |
[Ajax] 콜 이후 obj append (0) | 2019.01.25 |
Unrecognized token 'id': was expecting ('true', 'false' or 'null') (0) | 2018.10.15 |