Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@karosuwindam

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

More than 1 year has passed since last update.

はじめに

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

不満点

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

0
Help us understand the problem. What is going on with this article?
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
karosuwindam
適当に、気になったメモを記載します。 個人サークル「ウインダム工房」の代表 最近は、Golangのプログラムしか趣味で書いていないなぁ

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?