数组内的ruby排序哈希

时间:2016-10-12 17:35:15

标签: arrays ruby sorting hash

我有一系列哈希,我想根据:reference值排序。但是数组中的某些元素没有:reference键,因此无法排序。有没有办法忽略这个字段,只是对包含这个键的哈希元素进行排序?

我尝试了以下方法,但我收到了一个参数错误

ArgumentError: comparison of NilClass with String failed
  sort_by at org/jruby/RubyEnumerable.java:503



arr1 = [{:reference=> "F123",
            :name=> "test4"
          },
          {
            :reference=> "ZA4",
            :name=> "test3"
          },
          {
            :reference=> "A43",
            :name=> "test2"
          },
          {
            :name=> "test1"
          },
          {
            :name=> "homework1"
          }]

arr1 = arr1.sort_by { |hash| hash[:reference] }

puts arr1

正确的输出应如下所示:

=> arr1= [
        {:reference=>"A43", :name=>"test2"},
        {:reference=>"F123", :name=>"test4"},
        {:reference=>"ZA4", :name=>"test3"},
        {:name=> "test1"},
        {:name=> "homework1"}
      ]

2 个答案:

答案 0 :(得分:3)

您只能对可以比较的值进行排序,因此如果您拥有不同类型的值,最好先将它们转换为相同的类型。一个简单的解决方法是转换为字符串:

arr1.sort_by { |hash| hash[:reference].to_s }

您还可以指定默认值:

arr1.sort_by { |hash| hash[:reference] || '' }

修改:如果您希望最后排序的nil值:

arr1.sort_by { |hash| [ hash[:reference] ? 0 : 1, hash[:reference].to_s ] }

答案 1 :(得分:1)

如果你不介意临时变量,你可以将数组拆分成两个数组,一个包含带有ConstraintLayout's的哈希,另一个包含没有:

layout_width

然后排序第一个:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.constraint.ConstraintLayout
    android:layout_width="100dp"
    android:layout_height="300dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    android:background="#333333">

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline"
        app:layout_constraintGuide_percent="0.5"
        android:orientation="vertical"/>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="16dp"
        app:layout_constraintRight_toLeftOf="@+id/guideline"
        android:layout_marginRight="2dp"
        android:background="#FF0000">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:srcCompat="@mipmap/ic_launcher"
            tools:layout_editor_absoluteY="0dp"
            tools:layout_editor_absoluteX="0dp"/>
    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="16dp"
        android:layout_marginRight="16dp"
        app:layout_constraintLeft_toRightOf="@+id/guideline"
        android:layout_marginLeft="2dp"
        android:background="#00FF00">


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:srcCompat="@mipmap/ic_launcher"
            tools:layout_editor_absoluteY="0dp"
            tools:layout_editor_absoluteX="0dp"/>
    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

最后连接两个数组:

while ($row = mysqli_fetch_array($result)) {


                $a= date('Y/m/d H:i:s',strtotime($row['_date']));  //converting into required format of date



                $b=$row['Val'];

                if (++$counter == $numResults) {
                echo  '"'.$a.",".$b.'"'." ,";
                } else {
                echo  '"'.$a.",".$b.'\n"'." +". "\n";
                }



            };