Help us understand the problem. What is going on with this article?

RubyでルールのあるSortプログラムを作る

はじめに

rubyでオリジナルのソートを作るためのメモ
JSONが入っているのは自分のローカルサーバで作りやすかったから

コード

ruby.rb
require 'json'

$jsonfile = <<EOF
[{"id":643,"name":"7_to_7_3","tag":"ミットナイトレストラン7to703,胡桃ちの,漫画","created_at":"2018-12-02T11:30:34.013Z","updated_at":"2018-12-02T11:30:34.013Z"},
{"id":644,"name":"7_to_7_4","tag":"ミットナイトレストラン7to704,胡桃ちの,漫画","created_at":"2018-12-02T11:31:05.979Z","updated_at":"2018-12-02T11:31:05.979Z"},
{"id":641,"name":"7_to_7_10","tag":"ミットナイトレストラン7to710,胡桃ちの,漫画","created_at":"2018-11-29T12:01:43.992Z","updated_at":"2018-11-29T12:01:43.992Z"},
{"id":645,"name":"7_to_7_11","tag":"ミットナイトレストラン7to711,胡桃ちの,漫画","created_at":"2018-12-02T11:31:47.163Z","updated_at":"2018-12-02T11:31:47.163Z"}]
EOF

def num_kaiseki(str)
    output = []
    tmp = str
    num = str.rindex(/[0-9]/)
    out =""
    count = 0
    if (num !=nil)
        while(tmp[num-count])
            if (tmp[num-count]=="_")
            elsif (tmp[num-count].index(/[0-9]/)==nil)
                if(out[0]==".")
                    out = out[1..tmp.size]
                end
                break
            end
            if (tmp[num-count]=="_")
                out = "."+out
            else
                out = tmp[num-count]+out
            end
            count+=1
        end
    else
        num = tmp.size
    end
    output = [tmp[0..tmp.size-out.size-1] ,out.to_f]
end
def sort_jouken(a,b)
    t_a = num_kaiseki(a)
    t_b = num_kaiseki(b)
    if (t_a[0] == t_b[0])
        output = t_a[1]<=>t_b[1]
    else
        output = t_a[0]<=>t_b[0]
    end
    output
end

list = JSON.parse($jsonfile)
list.each do |word|
    p num_kaiseki(word["name"])
end
bb = list.sort { |a,b| sort_jouken(a["name"],b["name"]) }
bb.each do|word|
    print word["id"],":",word["name"],","
end

不満点

小数点のルール条件めちゃくちゃだから、どうにかしないと

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした