1

strong text

I have a box in a few boxes and placed inside each box for an hour. I want to sort by using the box clock named item. This sorting has three modes, the first ascending, the second descending, the third without sorting.

strong text

<body>

<style>
body{margin: 0 auto;padding: 0 auto;background: skyblue;}	
.full-item{width: 800px;height: 600px;margin: 50px auto;background: grey;}
.full-item .button-item{width: 100%;height: 80px;background: #B33771;}
.full-item .button-item button{margin: 30px 45%;}
.full-item .item-sort{width: 100%;height: 500px;background: white;margin-top: 10px;}
.full-item .item-sort:first-child{margin-top: 10px;}
.full-item .item-sort .item{width: 90%;height: 140px;background: red;margin: 10px auto;}
.item-sort .item .pic{width: 30%;height: 100%;background: #3B3B98;float: left;}
.item-sort .item .time{width: 70%;height: 100%;background: #1B9CFC;float: right;}
.item-sort .item .time span{color: white;text-align: center;display: block;line-height: 100px;}
</style>
<div class="full-item">
	<div class="button-item">
		<button id="Sort-item">Sort by</button>
	</div>
	<div class="item-sort">
		<div class="item">
			<div class="pic"></div>
			<div class="time"><span>15:20</span></div>
		</div>
		<div class="item">
			<div class="pic"></div>
			<div class="time"><span>13:10</span></div>
		</div>
		<div class="item">
			<div class="pic"></div>
			<div class="time"><span>18:40</span></div>
		</div>
	</div>
</div>

</body>

2
  • 2
    Instead of directly creating html and then sorting, you can have an array first and then on sorting type change, sort and render Commented Jan 1, 2019 at 9:29
  • I don't see the attempt. copy/paste error? Commented Jan 1, 2019 at 17:23

2 Answers 2

1

If the data is coming from JSON or other source, as with akbansa's recommendation, you should perform the sorting on the data first; otherwise, see below for an example of how you could reorder your elements:

const button = document.querySelector('#Sort-item')

// add handler
button.addEventListener('click', clickHandler)

// handler definition
function clickHandler(){
  let container = document.querySelector('.item-sort')
  let items = Array.from(container.querySelectorAll('.item-sort .item'))
  
  // sort based on time
  items = items.sort((a,b)=>{
    let a_time = a.querySelector('.time span').textContent
    let b_time = b.querySelector('.time span').textContent
    return a_time > b_time ? 1 : -1
  })
  
  // apply the order
  for(let item of items)
    container.appendChild(item)
}
body {
  margin: 0 auto;
  padding: 0 auto;
  background: skyblue;
}

.full-item {
  width: 800px;
  height: 600px;
  margin: 50px auto;
  background: grey;
}

.full-item .button-item {
  width: 100%;
  height: 80px;
  background: #B33771;
}

.full-item .button-item button {
  margin: 30px 45%;
}

.full-item .item-sort {
  width: 100%;
  height: 500px;
  background: white;
  margin-top: 10px;
}

.full-item .item-sort:first-child {
  margin-top: 10px;
}

.full-item .item-sort .item {
  width: 90%;
  height: 140px;
  background: red;
  margin: 10px auto;
}

.item-sort .item .pic {
  width: 30%;
  height: 100%;
  background: #3B3B98;
  float: left;
}

.item-sort .item .time {
  width: 70%;
  height: 100%;
  background: #1B9CFC;
  float: right;
}

.item-sort .item .time span {
  color: white;
  text-align: center;
  display: block;
  line-height: 100px;
}
<div class="full-item">
  <div class="button-item">
    <button id="Sort-item">Sort by</button>
  </div>
  <div class="item-sort">
    <div class="item">
      <div class="pic"></div>
      <div class="time"><span>15:20</span></div>
    </div>
    <div class="item">
      <div class="pic"></div>
      <div class="time"><span>13:10</span></div>
    </div>
    <div class="item">
      <div class="pic"></div>
      <div class="time"><span>18:40</span></div>
    </div>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

Update your html inside "button-item" class

<div class="button-item">
    <p>Sort By </p>
    <button id="sort-asc" onclick="app.sortAsc()">Asc</button>
    <button id="sort-desc" onclick="app.sortDesc()">Desc</button>
    <button id="reset" onclick="app.reset()">Reset</button>
</div>

Add to your scripts

var app = (function (){      
        var originalArr = []
        var timeArr = []
        var sortedArr = []
        var objArr = []

        var timeElements = document.querySelectorAll('.time')
        var itemSortElement = document.querySelector('.item-sort')

        for ( let timeEl of timeElements) {
            //  retrieving text from individual span element
            let timeText = timeEl.children[0].innerText;
            // retrieving parent node of div with class "time" 
            let timeParent = timeEl.parentNode
            let obj = { text: timeText, parent: timeParent }
            objArr.push(obj)        
            timeArr.push(timeText)      
        }

       // copying all elements/ texts from "timeArr" array to "originalArr" array 
       // to keep track of original order of texts 
       originalArr = timeArr.slice()

        function sortAsc () {
            // sorting the retrieved texts in ascending order
            sortedArr = timeArr.sort();         
            while (itemSortElement.hasChildNodes()) {   
              // removing all child elements of class "item-sort"
              itemSortElement.removeChild(itemSortElement.firstChild);
            }           
            for ( let i = 0; i < sortedArr.length; i++) {
                let filteredObj = objArr.filter((obj) => sortedArr[i] == obj.text)[0]
                let node = filteredObj.parent
                itemSortElement.appendChild(node)
            }
        }

        function sortDesc () {
            sortedArr = timeArr.sort().reverse();               
            while (itemSortElement.hasChildNodes()) {   
              itemSortElement.removeChild(itemSortElement.firstChild);
            }               
            for ( let i = 0; i < sortedArr.length; i++) {
                var filteredObj = objArr.filter((obj) => sortedArr[i] == obj.text)[0]   
                let node = filteredObj.parent
                itemSortElement.appendChild(node)
            }
        }


        function reset () {
          while (itemSortElement.hasChildNodes()) {   
              itemSortElement.removeChild(itemSortElement.firstChild);
            }               
            for ( let i = 0; i < originalArr.length; i++) {

                var filteredObj = objArr.filter((obj) => originalArr[i] == obj.text)[0] 
                let node = filteredObj.parent
                itemSortElement.appendChild(node)
            }
        }

         return {
            sortDesc,
            sortAsc,
            reset
         }

        })()

you can check it Demo

4 Comments

i want sort by through array. let FlyList = [{ "time": "15:20", }, { "time": "15:10", }, { "time": "18:40", }, { "time": "23:40", } ];
ok you did not mention that on the question. Are you still having issue in this question?
This is a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.