delivered the opinion of the Court.
We granted certiorari in this case to consider the constitutional validity of the statutory procedure under which petitioner was committed to a mental institution at the expiration of his criminal sentence in a state prison.
Petitioner, Johnnie K. Baxstrom, was convicted of second degree assault in April 1959 and was sentenced to a term of two and one-half to three years in a New York prison. On June 1, 1961, he was certified as insane by a prison physician. He was then transferred from prison to Dannemora State Hospital, an institution under the jurisdiction and control of the New York Department of Correction and used for the purpose of confining and caring for male- prisoners declared mentally ill while serving a criminal sentence. In November 1961, the director of Dannemora filed a petition in the Surrogate’s Court of Clinton County stating that Baxstrom’s penal sentence was about to terminate and requesting that he be civilly committed pursuant to § 384 of the New York Correction Law.
On December 6, 1961, a proceeding was held in the Surrogate’s chambers. Medical certificates were submitted by the State which stated that, in the opinion of two of its examining physicians, Baxstrom was still mentally ill and in need of hospital and institutional care. Respondent, then assistant director at Danne-mora, testified that in his opinion Baxstrom was still mentally ill. Baxstrom, appearing alone, was accorded *109a brief opportunity to ask questions.1 Respondent and the Surrogate both stated that they had no objection to his being transferred from Dannemora to a civil hospital under the jurisdiction of the Department of Mental Hygiene. But the Surrogate pointed out that he had no jurisdiction to determine that question — that under § 384 the decision was entirely up to the Department of Mental Hygiene. The Surrogate then signed a certificate which indicated he was satisfied that Baxstrom “may require mental care and treatment” in an institution for the mentally ill. The Department of Mental Hygiene had already determined ex parte that Baxstrom was not suitable for care in a civil hospital. Thus, on December 18, 1961, the date upon which Baxstrom’s penal sentence expired, custody over him shifted from the Department of Correction to the Department of Mental Hygiene, but he was retained at Dannemora and has remained there to this date.
Thereafter, Baxstrom sought a writ of habeas corpus in a state court. An examination by an independent psychiatrist was ordered and a hearing was held at which the examining psychiatrist testified that, in his opinion, Baxstrom was still mentally ill. The writ was dismissed. In 1963, Baxstrom applied again for a writ of habeas corpus, alleging that his constitutional rights had been violated and that he was then sane, or if insane, he should be transferred to a civil mental hospital. Due to his indigence and his incarceration in Dannemora, Baxstrom could not produce psychiatric testimony to disprove the testimony adduced at the prior hearing. The writ was therefore dismissed. Baxstrom’s alternative request for *110transfer to a civil mental hospital was again denied as being beyond the power of the court despite a statement by the State’s attorney that he wished that Baxstrom would be transferred to a civil mental hospital. On appeal to the Appellate Division, Third Department, the dismissal of the writ was affirmed without opinion. 21 App. Div. 2d 754. A motion for leave to appeal to the Court of Appeals was denied. 14 N. Y. 2d 490. We granted certiorari. 381 U. S. 949.
We hold that petitioner was denied equal protection of the laws by the statutory procedure under which a person may be civilly committed at the expiration of his penal sentence without the jury review available to all other persons civilly committed in New York. Petitioner was further denied equal protection of the laws by his civil commitment to an institution maintained by the Department of Correction beyond the expiration of his prison term without a judicial determination that he is dangerously mentally ill such as that afforded to all so committed except those, like Baxstrom, nearing the expiration of a penal sentence.
Section 384 of the New York Correction Law prescribes the procedure for civil commitment upon the expiration of the prison term of a mentally ill person confined in Dannemora.2 Similar procedures are prescribed for civil *111commitment of all other allegedly mentally ill persons. N. Y. Mental Hygiene Law §§ 70, 72. All persons civilly committed, however, other than those committed at the expiration of a penal term, are expressly granted the right to de novo review by jury trial of the question of their sanity under § 74 of the Mental Hygiene Law. Under this procedure any person dissatisfied with an order certifying him as mentally ill may demand full review by a jury of the prior determination as to his competency. If the jury returns a verdict that the person is sane, he must be immediately discharged. It follows that the State, having made this substantial review proceeding generally available on this issue, may not, consistent with the Equal Protection Clause of the Fourteenth Amendment, arbitrarily withhold it from some.
The director contends that the State has created a reasonable classification differentiating the civilly insane from the “criminally insane,” which he defines as those with dangerous or criminal propensities. Equal protection does not require that all persons be dealt with, identically, but it does require that a distinction made have some relevance to the purpose for which the classification is made. Walters v. City of St. Louis, 347 U. S. 231, 237. Classification of mentally ill persons as either insane or dangerously insane of course may be a reasonable distinction for purposes of determining the type of custodial or medical care to be given, but it has no relevance whatever in the context of the opportunity to show whether a person is mentally ill at all. For purposes of granting judicial review before a jury of the question whether a person is mentally ill and in need of institutionalization, there is no conceivable basis for distinguishing the com*112mitment of a person who is nearing the end of a penal term from all other civil commitments.
The statutory procedure provided in § 384 of the New York Correction Law denied Baxstrom the equal protection of the laws in another respect as well. Under § 384 the judge need only satisfy himself that the person “may require care and treatment in an institution for the mentally ill.” Having made such a finding, the decision whether to commit that person to a hospital maintained by the Department of Correction or to a civil hospital is completely in the hands of administrative officials.3 Except for persons committed to Dannemora upon expiration of sentence under § 384, all others civilly committed to hospitals maintained by the Department of *113Correction are committed only after judicial proceedings have been held in which it is determined that the person is so dangerously mentally ill that his presence in a civil hospital is dangerous to the safety of other patients or employees, or to the community.4
This statutory classification cannot be justified by the contention that Dannemora is substantially similar to other mental hospitals in the State and that commitment to one hospital or another is simply an administrative matter affecting no fundamental rights. The parties have described various characteristics of Dannemora to show its similarities and dissimilarities to civil hospitals in New York. As striking as the dissimilarities are, we need not make any factual determination as to the nature of Dannemora; the New York State Legislature has already made that determination. By statute, the hospital is under the jurisdiction of the Department of Correction and is used for the purpose of confining and caring for insane prisoners and persons, like Baxstrom, committed at the expiration of a penal term. N. Y. Correction Law § 375. Civil mental hospitals in New York, on the other hand, are under the jurisdiction and control of the Department of Mental Hygiene. Certain privileges of patients at Dannemora are restricted by statute. N. Y. Correction Law § 388. Moreover, as has *114been noted, specialized statutory procedures are prescribed for commitment to hospitals under the jurisdiction of the Department of Correction. While we may assume that transfer among like mental hospitals is a purely administrative function, where, as here, the State has created functionally distinct institutions, classification of patients for involuntary commitment to one of these institutions may not be wholly arbitrary.
The director argues that it is reasonable to classify persons in Baxstrom’s class together with those found to be dangerously insane since such persons are not only insane but have proven criminal tendencies as shown by their past criminal records. He points to decisions of the New York Court of Appeals supporting this view. People ex rel. Kamisaroff v. Johnston, 13 N. Y. 2d 66, 192 N. E. 2d 11; People ex rel. Brunson v. Johnston, 15 N. Y. 2d 647, 204 N. E. 2d 200.
We find this contention untenable. Where the State has provided for a judicial proceeding to determine the dangerous propensities of all others civilly committed to an institution of the Department of Correction, it may not deny this right to a person in Baxstrom’s position solely on the ground that he was nearing the expiration of a prison term.5 It may or may not be that Baxstrom *115is presently mentally ill and such a danger to others that the strict security of a Department of Correction hospital is warranted. All others receive a judicial hearing on this issue. Equal protection demands that Baxstrom receive the same.
The capriciousness of the classification employed by the State is thrown sharply into focus by the fact that the full benefit of a judicial hearing to determine dangerous tendencies is withheld only in the case of civil commitment of one awaiting expiration of penal sentence. A person with a past criminal record is presently entitled to a hearing on the question whether he is dangerously mentally ill so long as he is not in prison at the time civil commitment proceedings are instituted. Given this distinction, all semblance of rationality of the classification, purportedly based upon criminal propensities, disappears.
In order to accord to petitioner the equal protection of the laws, he was and is entitled to a review of the determination as to his sanity in conformity with proceedings granted all others civilly committed under § 74 of the New York Mental Hygiene Law. He is also entitled to a hearing under the procedure granted all others by § 85 of the New York Mental Hygiene Law to determine whether he is so dangerously mentally ill that he must remain in a hospital maintained by the Department of Correction. The judgment of the Appellate Division of the Supreme Court, in the Third Judicial Department of New York is reversed and the case is remanded to that court for further proceedings not inconsistent with this opinion.
It is so ordered.
Mr. Justice Black concurs in the result.