<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ko">
	<id>https://smwiki.info/index.php?action=history&amp;feed=atom&amp;title=Javascript%3A%ED%91%9C_%EC%A0%95%EB%A0%AC</id>
	<title>Javascript:표 정렬 - 편집 역사</title>
	<link rel="self" type="application/atom+xml" href="https://smwiki.info/index.php?action=history&amp;feed=atom&amp;title=Javascript%3A%ED%91%9C_%EC%A0%95%EB%A0%AC"/>
	<link rel="alternate" type="text/html" href="https://smwiki.info/index.php?title=Javascript:%ED%91%9C_%EC%A0%95%EB%A0%AC&amp;action=history"/>
	<updated>2026-05-05T10:28:34Z</updated>
	<subtitle>이 문서의 편집 역사</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://smwiki.info/index.php?title=Javascript:%ED%91%9C_%EC%A0%95%EB%A0%AC&amp;diff=7950&amp;oldid=prev</id>
		<title>Sam: 새 문서: {{Javascript}}  == 개요 == 표의 내용에 따라 오름차순, 내림차순의 정렬. &lt;br /&gt;  = 정렬 스크립트 = https://www.w3schools.com/howto/howto_js_sort_table.asp&lt;nowi...</title>
		<link rel="alternate" type="text/html" href="https://smwiki.info/index.php?title=Javascript:%ED%91%9C_%EC%A0%95%EB%A0%AC&amp;diff=7950&amp;oldid=prev"/>
		<updated>2022-11-21T08:50:56Z</updated>

		<summary type="html">&lt;p&gt;새 문서: {{Javascript}}  == 개요 == 표의 내용에 따라 오름차순, 내림차순의 정렬. &amp;lt;br /&amp;gt;  = 정렬 스크립트 = https://www.w3schools.com/howto/howto_js_sort_table.asp&amp;lt;nowi...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;새 문서&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Javascript}}&lt;br /&gt;
&lt;br /&gt;
== 개요 ==&lt;br /&gt;
표의 내용에 따라 오름차순, 내림차순의 정렬.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 정렬 스크립트 =&lt;br /&gt;
https://www.w3schools.com/howto/howto_js_sort_table.asp&amp;lt;nowiki/&amp;gt;의 코드를 참고하였다.&lt;br /&gt;
&lt;br /&gt;
취향에 따라 구현 방법은 다르겠지만.. 테이블 태그 안에서 함수를 호출하여 사용하자.&amp;lt;syntaxhighlight lang=&amp;quot;html&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;table class=&amp;quot;table table-hover sortable&amp;quot; id=&amp;quot;jb-table&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead class=&amp;quot;table-dark&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;tr&amp;gt;&lt;br /&gt;
        &amp;lt;th onclick=&amp;quot;sortTable(0)&amp;quot;&amp;gt;문항&amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;th onclick=&amp;quot;sortTable_int(1)&amp;quot;&amp;gt;점수&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 일반 텍스트 ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&lt;br /&gt;
function sortTable(n) {&lt;br /&gt;
  var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;&lt;br /&gt;
  table = document.getElementById(&amp;quot;jb-table&amp;quot;);&lt;br /&gt;
  switching = true;&lt;br /&gt;
  // Set the sorting direction to ascending:&lt;br /&gt;
  dir = &amp;quot;asc&amp;quot;;&lt;br /&gt;
  /* Make a loop that will continue until&lt;br /&gt;
  no switching has been done: */&lt;br /&gt;
  while (switching) {&lt;br /&gt;
    // Start by saying: no switching is done:&lt;br /&gt;
    switching = false;&lt;br /&gt;
    rows = table.rows;&lt;br /&gt;
    /* Loop through all table rows (except the&lt;br /&gt;
    first, which contains table headers): */&lt;br /&gt;
    // i값으로 몇행부터 정렬 할지 정한다.&lt;br /&gt;
    for (i = 3; i &amp;lt; (rows.length - 1); i++) {&lt;br /&gt;
      // Start by saying there should be no switching:&lt;br /&gt;
      shouldSwitch = false;&lt;br /&gt;
      /* Get the two elements you want to compare,&lt;br /&gt;
      one from current row and one from the next: */&lt;br /&gt;
      x = rows[i].getElementsByTagName(&amp;quot;TD&amp;quot;)[n];&lt;br /&gt;
      y = rows[i + 1].getElementsByTagName(&amp;quot;TD&amp;quot;)[n];&lt;br /&gt;
      /* Check if the two rows should switch place,&lt;br /&gt;
      based on the direction, asc or desc: */&lt;br /&gt;
      if (dir == &amp;quot;asc&amp;quot;) {&lt;br /&gt;
        if (x.innerHTML.toLowerCase() &amp;gt; y.innerHTML.toLowerCase()) {&lt;br /&gt;
          // If so, mark as a switch and break the loop:&lt;br /&gt;
          shouldSwitch = true;&lt;br /&gt;
          break;&lt;br /&gt;
        }&lt;br /&gt;
      } else if (dir == &amp;quot;desc&amp;quot;) {&lt;br /&gt;
        if (x.innerHTML.toLowerCase() &amp;lt; y.innerHTML.toLowerCase()) {&lt;br /&gt;
          // If so, mark as a switch and break the loop:&lt;br /&gt;
          shouldSwitch = true;&lt;br /&gt;
          break;&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    if (shouldSwitch) {&lt;br /&gt;
      /* If a switch has been marked, make the switch&lt;br /&gt;
      and mark that a switch has been done: */&lt;br /&gt;
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);&lt;br /&gt;
      switching = true;&lt;br /&gt;
      // Each time a switch is done, increase this count by 1:&lt;br /&gt;
      switchcount ++;&lt;br /&gt;
    } else {&lt;br /&gt;
      /* If no switching has been done AND the direction is &amp;quot;asc&amp;quot;,&lt;br /&gt;
      set the direction to &amp;quot;desc&amp;quot; and run the while loop again. */&lt;br /&gt;
      if (switchcount == 0 &amp;amp;&amp;amp; dir == &amp;quot;asc&amp;quot;) {&lt;br /&gt;
        dir = &amp;quot;desc&amp;quot;;&lt;br /&gt;
        switching = true;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 숫자 ===&lt;br /&gt;
11, 5 등의 숫자데이터는 자바스크립트에서 자료형을 바꿔주지 않으면 텍스트로 처리하여 정렬해 오름차순에서 11보다 5가 큰 것으로 인식한다.&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&lt;br /&gt;
function sortTable_int(n) {&lt;br /&gt;
  var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;&lt;br /&gt;
  table = document.getElementById(&amp;quot;jb-table&amp;quot;);&lt;br /&gt;
  switching = true;&lt;br /&gt;
  // Set the sorting direction to ascending:&lt;br /&gt;
  dir = &amp;quot;asc&amp;quot;;&lt;br /&gt;
  /* Make a loop that will continue until&lt;br /&gt;
  no switching has been done: */&lt;br /&gt;
  while (switching) {&lt;br /&gt;
    // Start by saying: no switching is done:&lt;br /&gt;
    switching = false;&lt;br /&gt;
    rows = table.rows;&lt;br /&gt;
    /* Loop through all table rows (except the&lt;br /&gt;
    first, which contains table headers): */&lt;br /&gt;
    // i값으로 몇행부터 할지 정한다.&lt;br /&gt;
    for (i = 3; i &amp;lt; (rows.length - 1); i++) {&lt;br /&gt;
      // Start by saying there should be no switching:&lt;br /&gt;
      shouldSwitch = false;&lt;br /&gt;
      /* Get the two elements you want to compare,&lt;br /&gt;
      one from current row and one from the next: */&lt;br /&gt;
      x = rows[i].getElementsByTagName(&amp;quot;TD&amp;quot;)[n];&lt;br /&gt;
      y = rows[i + 1].getElementsByTagName(&amp;quot;TD&amp;quot;)[n];&lt;br /&gt;
      x = parseInt(x.innerHTML)&lt;br /&gt;
      y = parseInt(y.innerHTML)&lt;br /&gt;
        /* Check if the two rows should switch place,&lt;br /&gt;
      based on the direction, asc or desc: */&lt;br /&gt;
      if (dir == &amp;quot;asc&amp;quot;) {&lt;br /&gt;
        if (x &amp;gt; y) {&lt;br /&gt;
          // If so, mark as a switch and break the loop:&lt;br /&gt;
          shouldSwitch = true;&lt;br /&gt;
          break;&lt;br /&gt;
        }&lt;br /&gt;
      } else if (dir == &amp;quot;desc&amp;quot;) {&lt;br /&gt;
        if (x &amp;lt; y) {&lt;br /&gt;
          // If so, mark as a switch and break the loop:&lt;br /&gt;
          shouldSwitch = true;&lt;br /&gt;
          break;&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    // 행을 바꾸는 동작.&lt;br /&gt;
    if (shouldSwitch) {&lt;br /&gt;
      /* If a switch has been marked, make the switch&lt;br /&gt;
      and mark that a switch has been done: */&lt;br /&gt;
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);&lt;br /&gt;
      switching = true;&lt;br /&gt;
      // Each time a switch is done, increase this count by 1:&lt;br /&gt;
      switchcount ++;&lt;br /&gt;
    } else {&lt;br /&gt;
      /* If no switching has been done AND the direction is &amp;quot;asc&amp;quot;,&lt;br /&gt;
      set the direction to &amp;quot;desc&amp;quot; and run the while loop again. */&lt;br /&gt;
      if (switchcount == 0 &amp;amp;&amp;amp; dir == &amp;quot;asc&amp;quot;) {&lt;br /&gt;
        dir = &amp;quot;desc&amp;quot;;&lt;br /&gt;
        switching = true;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sam</name></author>
	</entry>
</feed>