質問編集履歴

4

試したコードを追加

2018/11/30 07:12

投稿

stakezaki
stakezaki

スコア46

test CHANGED
File without changes
test CHANGED
@@ -36,4 +36,152 @@
36
36
 
37
37
 
38
38
 
39
+ ヒントをもらって実装してみましたがダメでした。
40
+
41
+
42
+
43
+ ```java
44
+
45
+ import java.util.ArrayList;
46
+
47
+ import java.util.Collections;
48
+
49
+ import java.util.Comparator;
50
+
51
+ import java.util.HashMap;
52
+
53
+ import java.util.List;
54
+
55
+ import java.util.Map;
56
+
57
+ import java.util.Map.Entry;
58
+
59
+
60
+
61
+ public class DescTest {
62
+
63
+
64
+
65
+ public static void main(String[] args) {
66
+
67
+ // String[] src = {"a", "b", "A", "abc", "def", "ab", "abcd", "あいうえお", "かきくけこ"};
68
+
69
+ String[] src = {"a", "b", "c", "d", "e", "f", "g", "h", "i"};
70
+
71
+ Map<Integer,String> dist = new HashMap<Integer,String>();
72
+
73
+
74
+
75
+ for(int i=0;i<src.length;i++) {
76
+
77
+ dist.put(i, DescTest.decKey(src[i]));
78
+
79
+ // dist.put(i, src[i]);
80
+
81
+ }
82
+
83
+
84
+
85
+ List<Map.Entry<Integer,String>> entries =
86
+
87
+ new ArrayList<Map.Entry<Integer,String>>(dist.entrySet());
88
+
89
+ Collections.sort(entries, new Comparator<Map.Entry<Integer,String>>() {
90
+
91
+
92
+
93
+ @Override
94
+
95
+ public int compare(
96
+
97
+ Entry<Integer,String> entry1, Entry<Integer,String> entry2) {
98
+
99
+ return (entry1.getValue()).compareTo(entry2.getValue());
100
+
101
+ }
102
+
103
+ });
104
+
105
+
106
+
107
+ for (Entry<Integer,String> s : entries) {
108
+
109
+ System.out.println("s.getKey() : " + s.getKey());
110
+
111
+ System.out.println("s.getValue() : " + src[s.getKey()]);
112
+
113
+ }
114
+
115
+ }
116
+
117
+
118
+
119
+ public static String decKey(String s) {
120
+
121
+ char[] c = s.toCharArray();
122
+
123
+ int v = c[0];
124
+
125
+ // System.out.print("c="+v+" ");
126
+
127
+ for(int i = 0; i < c.length; i++) {
128
+
129
+ c[i] = (char)(65535 - c[i]);
130
+
131
+ }
132
+
133
+ return c.toString();
134
+
135
+ }
136
+
137
+
138
+
139
+ }
140
+
141
+ ```
142
+
143
+ 結果(降順にならない)
144
+
145
+ ```
146
+
147
+ s.getValue() : b
148
+
149
+ s.getKey() : 4
150
+
151
+ s.getValue() : e
152
+
153
+ s.getKey() : 8
154
+
155
+ s.getValue() : i
156
+
157
+ s.getKey() : 6
158
+
159
+ s.getValue() : g
160
+
161
+ s.getKey() : 0
162
+
163
+ s.getValue() : a
164
+
165
+ s.getKey() : 7
166
+
167
+ s.getValue() : h
168
+
169
+ s.getKey() : 3
170
+
171
+ s.getValue() : d
172
+
173
+ s.getKey() : 2
174
+
175
+ s.getValue() : c
176
+
177
+ s.getKey() : 5
178
+
179
+ s.getValue() : f
180
+
181
+ ```
182
+
183
+
184
+
185
+
186
+
39
187
  よろしくお願いします。

3

そもそもの要件

2018/11/30 07:12

投稿

stakezaki
stakezaki

スコア46

test CHANGED
File without changes
test CHANGED
@@ -32,4 +32,8 @@
32
32
 
33
33
 
34
34
 
35
+ そもそもの要件として、昇順しかソートできないKVSで降順を実現するために、降順用に項目を1つ追加し、降順の値を格納してそれをKVSの機能で昇順ソートすることで降順を実現したいというものです。
36
+
37
+
38
+
35
39
  よろしくお願いします。

2

数値の場合の例を追記

2018/11/29 10:59

投稿

stakezaki
stakezaki

スコア46

test CHANGED
File without changes
test CHANGED
@@ -20,4 +20,16 @@
20
20
 
21
21
 
22
22
 
23
+ これは、入力値を関数内でソートするようなものではなく、関数は複数回呼ばれますが、その値が結果的に降順になるというものです。
24
+
25
+
26
+
27
+ 例えば、xがlongの最大値よりも小さい数字の場合、関数は以下のように書けます。
28
+
29
+ java.lang.Long.MAX_VALUE-Long.parseLong(x)
30
+
31
+ これを、xが文字列の場合でも求めたいのです。
32
+
33
+
34
+
23
35
  よろしくお願いします。

1

返す値は文字列で構いません

2018/11/29 10:21

投稿

stakezaki
stakezaki

スコア46

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  表題のような関数をjavaで作成したいと考えています。
2
2
 
3
- 文字列xに対して降順となる数値aを返す関数です。
3
+ 文字列xに対して降順となる数値もしくは文字列aを返す関数です。
4
4
 
5
5
  f(x) = a
6
6