首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >按照1-9和a-z显示数组的顺序

按照1-9和a-z显示数组的顺序
EN

Stack Overflow用户
提问于 2018-05-27 20:05:13
回答 1查看 103关注 0票数 0

我已经创建了一个动态金字塔。

  1. 用户可以添加和删除块,这些块将自动使用数字字母组合命名,请参阅所附图片。
  2. 我已将该组合保存并排序在数组
  3. (3) "4p","5i","6g“

]1

问题:

  1. 我想在每次用户点击“添加区块”时,以正确的顺序显示区块,即1-9和a-z。正如你在图片上看到的,目前6g先于4P。

有什么方法可以做到这一点吗?

我一直在尝试通过以下方式来实现这一点

 function newOrder() {
    var oldOrder = values;
    values.sort();
    document.getElementByClassName('.values').innerHTML = values;
  }

$(document).ready(function() {
  //Add Block Functionality
  let values = [];

  $('#add-block .button').click(function() {
    //determin widht of last div
    var lastwidth = $('.pyramid li:last-child .item').width();

    //calculation of next div
    if (lastwidth == null) {
      var plus = 90;
    } else {
      var plus = lastwidth + 190; //not sure why 190 but with this value they line up smoothly. Was expecting 0 and 100 for the values.
    }

    //create radom number
    var number = Math.floor(Math.random() * 9) + 1;

    //create radom letter
    function randLetter() {
      var letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
      var letter = letters[Math.floor(Math.random() * letters.length)];

      return letter
    }

    //make letter available globally
    var resultLetter = randLetter();

    //create radom color
    function randColor() {
      var colors = ["green", "yellowgreen", "Chocolate", "goldenrod", "cadetblue", "firebrick", "magenta", "LightSeaGreen", "Peru", "Sienna", "SlateBlue", "Snow", "Tan", "Skyblue"];
      var color = colors[Math.floor(Math.random() * colors.length)];

      return color
    }

    //make color available gloabally
    var resultColor = randColor();
    var $block = $('<li><div class="item" style="width:' + plus + 'px; border-bottom: 60px solid ' + resultColor + ' ;"><input class="values" type="text" placeholder=" ' + number + resultLetter + ' " maxlength="2"> </div></li>');

    $('.pyramid').append($block);

    //save values

    values.push(number + resultLetter);
    values.sort();
    console.log(values);
});

  //Remove Block Functionality
  $('#remove-block .button').click(function() {
    value = $(".values", $('.pyramid li').last()).attr("placeholder").trim()//find last value added in pyramid//.attr()value of attribute placeholder,trim() is just for white space
    values.splice(values.indexOf(value), 1)//indexOf() method returns the position of the first occurrence of a specified value in a string. In this case it is the index of value, which is the last item in the array. Could be replaced by -1 I think
    console.log(values)
    $('.pyramid li').last().remove();
  })
});
body, html {
  box-sizing: border-box;
  font-size: 16px;
  font-family: 'Open Sans', sans-serif;
  background-color: #101935;

}
*, *:before, *:after {
  box-sizing: inherit;
}

ul, li {
  list-style: none;
  margin: 0;
  padding: 0;
}

li div.item {
  margin: 0 auto;
  position: relative;
  height: 0px;
  width:  100px;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 60px solid #0488e0;
  display: flex;
  justify-content: center;
}

  li div.item:not(.item0){
  border-bottom: 60px solid #0488e0;
  border-left: 45px solid transparent;
  border-right: 45px solid transparent;
  margin-top: 10px;
  height: 0;
}

.values {
  background: rgba(255, 255, 255, .6);
  border: none;
  text-align: center;
  font-size: 15px;
  color: black;
  font-weight: bold;
  height: 20px;
  width: 35px;
  border-radius: 2px;
  position: absolute;
  top: 30px;
}

.values:focus {
background: rgba(255, 255, 255, .95);
box-shadow: 0 2px 2px rgba(0, 0, 0, .15);
outline: none;
}

/*buttons section */
.buttons, #add-block, #remove-block{
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
}
#add-block span, #remove-block span {
  background-color: #edf7f6;
  padding: 5px 15px ;
  font-size: 18px;
  border-radius: 2px;
  color:#888;
  font-weight: 400;

}
#add-block .button, #remove-block .button{
  background-color: #0488e0;
  padding: 5px;
  border-radius: 2px;
  width: 40px;
  text-align: center;
  display: block;
  font-size: 20px;
  color: #ffffff;
  margin: 10px;
  transition: background-color 250ms ease-in-out 100ms;
}

#add-block .button:hover, #remove-block .button:hover{
  background-color: #059BFF;
  cursor: pointer;
}
  <body>

    <ul class="pyramid">
    </ul>

    <section class="buttons">
      <div id="add-block">
        <span>Add Block</span>
        <div class="button">+
        </div>
      </div>
      <div id="remove-block">
        <span>Remove Block</span>
        <div class="button">-
        </div>
      </div>
    </section>


  <script
    src="https://code.jquery.com/jquery-3.3.1.min.js"
    integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
    crossorigin="anonymous"></script>
  <script src="resources/js/main.js"></script>

  </body>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-27 21:14:41

您需要解决的核心问题是拥有一个排序数组,其中1-数字应该是从高到低的顺序2-如果两个数字相同,则应考虑它们的字母表,即从高到低

您可以使用数组的内置排序函数并提供自定义比较器来实现这一点

注意:除了连接数字和字母表之外,您还可以创建一个具有两个属性的对象,这将有助于编写简化的比较器,同时在html中呈现时,您始终可以连接这两个属性。

var myStrings=[{num:'1',char:'z'},{num:'9',char:'a'},{num:'1',char:'b'},{num:'9',char:'d'}]

function myCustomComparator(a,b){
if (a.num<b.num) {
    return -1;
  }
  else if (a.num>b.num) {
    return 1;
  }
  // a.num must be equal to b.num
  if (a.char<b.char) {
    return -1;
  }
  else if (a.char>b.char) {
    return 1;
  }
  //char must be equal too
  return 0;
}
myString.sort(myCustomComparator);

虽然上面的代码还没有经过测试,但是您现在应该已经知道如何使用自定义比较器来实现您想要的结果,您可以进一步阅读here

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50551953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档